KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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, the Broadcast is never received. What do I do wrong? Sending the Broadcast public class SendBroadcast extends Activity { public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void sendBroadcast(View v){ Intent broadcast = new Intent(); broadcast.setAction(BROADCAST_ACTION); sendBroadcast(broadcast); } } Receiving it public class ToastDisplay extends Activity { private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT); } }; @Override protected void onResume() { IntentFilter filter = new IntentFilter(); filter.addAction(SendBroadcast.BROADCAST_ACTION); registerReceiver(receiver, filter); super.onResume(); } @Override protected void onPause() { unregisterReceiver(receiver); super.onPause(); } } Manifest <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SendBroadcast" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ToastDisplay">
Tags (comma-separated)
Save Edits
Cancel