I just try this small sample of the project, all that it does: In the first operation there is a button that sends the broadcast. The second operation shows a toast when it is received. Below is the code, broadcast not received. What am I doing wrong?
Broadcast Submission
public class SendBroadcast extends Activity { public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST"; @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); } }
Getting
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"> <intent-filter> <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"></action> </intent-filter> </activity> </application>
android broadcastreceiver
nheid Dec 29 '10 at 15:09 2010-12-29 15:09
source share