So, I'm trying to get a handle on BroadcastReceivers and Intent filters. I have a custom Dialog that I create in MyActivity. In the Dialog, I have a Button. When the button gets clicked, I want to send a broadcast that MyActivity's receiver will pick up. Here's what I have right now:
//MyActivity.java
class myActivity extends Activity {
//MyDialog dialog initialized in onCreate
...
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//toast "Broadcast received"
}
}
}
//MyDialog.java
class MyDialog extends Dialog {
//m_context = incoming context from MyActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnCLickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("android.intent.action.RUN");
m_context.sendBroadcast(intent);
}
});
}
}
//AndroidManifest.xml
<activity android:name=".MyActivity" />
<receiver android:name="MyReceiver" android:enabled="true">
<intent-filter >
<action android:name="android.intent.action.RUN"/>
</intent-filter>
</receiver>
When I press button1, the app crashes. Can anyone lead me in the right direction?
It would probably be much easier to register your receiver in code in your activity's onResume method (and unregiser in onPause).
in MyActivity do something like this:
and to invoke broadcast