Android BroadcastReceiver within Activity

后端 未结 7 1432
后悔当初
后悔当初 2020-11-28 23:59

I\'m just trying this little sample project, all it does: Activity one has a Button that sends a Broadcast. Activity two displays a toast when received. Below is the code, t

相关标签:
7条回答
  • 2020-11-29 00:06

    I think your problem is that you send the broadcast before the other activity start ! so the other activity will not receive anything .

    1. The best practice to test your code is to sendbroadcast from thread or from a service so the activity is opened and its registered the receiver and the background process sends a message.
    2. start the ToastDisplay activity from the sender activity ( I didn't test that but it may work probably )
    0 讨论(0)
  • 2020-11-29 00:11

    What do I do wrong?

    The source code of ToastDisplay is OK (mine is similar and works), but it will only receive something, if it is currently in foreground (you register receiver in onResume). But it can not receive anything if a different activity (in this case SendBroadcast activity) is shown.

    Instead you probably want to startActivity ToastDisplay from the first activity?

    BroadcastReceiver and Activity make sense in a different use case. In my application I need to receive notifications from a background GPS tracking service and show them in the activity (if the activity is in the foreground).

    There is no need to register the receiver in the manifest. It would be even harmful in my use case - my receiver manipulates the UI of the activity and the UI would not be available during onReceive if the activity is not currently shown. Instead I register and unregister the receiver for activity in onResume and onPause as described in BroadcastReceiver documentation:

    You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml.

    0 讨论(0)
  • 2020-11-29 00:11

    You need to define the receiver as a class in the manifest and it will receive the intent:

    <application
      ....
      <receiver android:name=".ToastReceiver">
        <intent-filter>
          <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
        </intent-filter>
      </receiver>
    </application>
    

    And you don't need to create the class manually inside ToastDisplay.

    In the code you provided, you must be inside ToastDisplay activity to actually receive the Intent.

    0 讨论(0)
  • 2020-11-29 00:13
     Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT);
    

    makes the toast, but doesnt show it.

    You have to do Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();

    0 讨论(0)
  • 2020-11-29 00:16

    You forget to write .show() at the end, which is used to show the toast message.

    Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
    

    It is a common mistake that programmer does, but i am sure after this you won't repeat the mistake again... :D

    0 讨论(0)
  • 2020-11-29 00:23

    Your also have to register the receiver in onCreate(), like this:

    IntentFilter filter = new IntentFilter();
    filter.addAction("csinald.meg");
    registerReceiver(receiver, filter);
    
    0 讨论(0)
提交回复
热议问题