Skip to content

Commit

Permalink
fixed localbroadcast problem. Updated as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
JimSeker committed Jul 30, 2017
1 parent df1e047 commit 0ead150
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 131 deletions.
5 changes: 4 additions & 1 deletion BroadCastDemo1/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 23 additions & 34 deletions BroadCastDemo1/app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<facet type="android" name="Android">
<configuration>
<option name="SELECTED_BUILD_VARIANT" value="debug" />
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<afterSyncTasks>
Expand Down Expand Up @@ -47,73 +46,63 @@
<sourceFolder url="file:https://$MODULE_DIR$/src/debug/assets" type="java-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/debug/aidl" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/debug/java" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/debug/jni" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/debug/shaders" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/testDebug/res" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/testDebug/resources" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/testDebug/assets" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/testDebug/aidl" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/testDebug/java" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/testDebug/jni" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/testDebug/rs" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/testDebug/shaders" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/main/res" type="java-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/main/assets" type="java-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file:https://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/24.2.1/jars" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/24.2.1/jars" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-compat/24.2.1/jars" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-core-ui/24.2.1/jars" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-core-utils/24.2.1/jars" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-fragment/24.2.1/jars" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-media-compat/24.2.1/jars" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/24.2.1/jars" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-vector-drawable/24.2.1/jars" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file:https://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file:https://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file:https://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 24 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-annotations-24.2.1" level="project" />
<orderEntry type="library" exported="" name="support-v4-24.2.1" level="project" />
<orderEntry type="library" exported="" name="support-compat-24.2.1" level="project" />
<orderEntry type="library" exported="" name="support-media-compat-24.2.1" level="project" />
<orderEntry type="library" exported="" name="animated-vector-drawable-24.2.1" level="project" />
<orderEntry type="library" exported="" name="support-fragment-24.2.1" level="project" />
<orderEntry type="library" exported="" name="support-core-ui-24.2.1" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-24.2.1" level="project" />
<orderEntry type="library" exported="" name="support-vector-drawable-24.2.1" level="project" />
<orderEntry type="library" exported="" name="support-core-utils-24.2.1" level="project" />
<orderEntry type="library" exported="" name="support-core-ui-25.4.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-25.4.0" level="project" />
<orderEntry type="library" exported="" name="support-media-compat-25.4.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-25.4.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-25.4.0" level="project" />
<orderEntry type="library" exported="" name="support-vector-drawable-25.4.0" level="project" />
<orderEntry type="library" exported="" name="support-compat-25.4.0" level="project" />
<orderEntry type="library" exported="" name="support-core-utils-25.4.0" level="project" />
<orderEntry type="library" exported="" name="support-fragment-25.4.0" level="project" />
<orderEntry type="library" exported="" name="animated-vector-drawable-25.4.0" level="project" />
</component>
</module>
10 changes: 5 additions & 5 deletions BroadCastDemo1/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
compileSdkVersion 25
buildToolsVersion "25.0.3"

defaultConfig {
applicationId "edu.cs4730.broadcastdemo1"
minSdkVersion 14
targetSdkVersion 24
minSdkVersion 16
targetSdkVersion 25
versionCode="1"
versionName="1.0"
}
Expand All @@ -22,5 +22,5 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:appcompat-v7:25.4.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

Expand All @@ -15,47 +16,47 @@

public class MainActivity extends AppCompatActivity {

//declare the intent names here, except if change ACTION1, fix it in AndroidManifest.xml as well.
public static final String ACTION1 = "edu.cs4730.bcr.mystaticevent";
public static final String ACTION2 = "edu.cs4730.bcr.mydyanicevent";
String TAG = "MainActivity";
MyReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment()).commit();
}
//need to initialize the variable here.
mReceiver = new MyReceiver();
}

@Override
public void onResume() {
super.onResume();
// Register mReceiver to receive messages.
// the local is not working, I don't know why... Using general one.
//LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(ACTION2));

registerReceiver(mReceiver, new IntentFilter(ACTION2));
//Log.v(TAG, "receiver should be registered");
}
@Override
protected void onPause() { //or onDestory()
// Unregister since the activity is not visible
//again, local not working,
//LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
unregisterReceiver(mReceiver);
Log.v(TAG, "receiver should be unregistered");
super.onPause();

}
//declare the intent names here, except if change ACTION1, fix it in AndroidManifest.xml as well.
public static final String ACTION1 = "edu.cs4730.bcr.mystaticevent";
public static final String ACTION2 = "edu.cs4730.bcr.mydyanicevent";

String TAG = "MainActivity";

MyReceiver mReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment()).commit();
}

//need to initialize the variable here.
mReceiver = new MyReceiver();

}

@Override
public void onResume() {
super.onResume();
// Register mReceiver to receive messages. This is using the local broadcast, instead of a global.
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(ACTION2));
//the one below registers a global receiver.
//registerReceiver(mReceiver, new IntentFilter(ACTION2));
//Log.v(TAG, "receiver should be registered");
}

@Override
protected void onPause() { //or onDestory()
// Unregister since the activity is not visible. This is using the local broadcast, instead of a global.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
//the one below unregisters a global receiver.
//unregisterReceiver(mReceiver);
Log.v(TAG, "receiver should be unregistered");
super.onPause();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -11,42 +12,45 @@

/**
* This is code demo to send two different custom intents.
*
*
*/
public class MainFragment extends Fragment {

String TAG= "MainFragment";

public MainFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myView = inflater.inflate(R.layout.fragment_main, container, false);

//setup button to send an intent for static registered receiver.
myView.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.ACTION1);
getActivity().sendBroadcast(i);
}
});

//setup button to send an intent for dynamic registered receiver, which is registered in MainActivity.
myView.findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.ACTION2);
getActivity().sendBroadcast(i);
Log.v(TAG, "Should have sent the broadcast.");
}
});
return myView;
}
String TAG = "MainFragment";

public MainFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myView = inflater.inflate(R.layout.fragment_main, container, false);

//setup button to send an intent for static registered receiver.
myView.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.ACTION1);
//since it's registered as a global (in the manifest), use sendBroadCast
//LocalBroadcastManager.getInstance(getContext()).sendBroadcast(i);
getActivity().sendBroadcast(i);
}
});

//setup button to send an intent for dynamic registered receiver, which is registered in MainActivity.
myView.findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.ACTION2);
//since it's registered a local, use the LocalBroadcastManager.
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(i);
//use this if registered as a global receiver.
//getActivity().sendBroadcast(i);
Log.v(TAG, "Should have sent the broadcast.");
}
});
return myView;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Received an intent.", Toast.LENGTH_SHORT).show();
if (intent.getAction().equals(MainActivity.ACTION1)){ //is it our action1?
Toast.makeText(context, "We received an intent for Action1.", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(MainActivity.ACTION2)){ //is it our action2?
Toast.makeText(context, "We received an intent for Action2.", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onReceive(Context context, Intent intent) {

Toast.makeText(context, "Received an intent.", Toast.LENGTH_SHORT).show();

if (intent.getAction().equals(MainActivity.ACTION1)) { //is it our action1?
Toast.makeText(context, "We received an intent for Action1.", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(MainActivity.ACTION2)) { //is it our action2?
Toast.makeText(context, "We received an intent for Action2.", Toast.LENGTH_SHORT).show();
}
}
}
5 changes: 4 additions & 1 deletion BroadCastDemo1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.3.3'
}
}

allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
4 changes: 2 additions & 2 deletions BroadCastDemo1/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Oct 20 14:34:25 MDT 2016
#Sat Jul 29 18:00:23 MDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\:https://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\:https://services.gradle.org/distributions/gradle-3.3-all.zip

0 comments on commit 0ead150

Please sign in to comment.