How to use registerReceiver method?
I want to use dynamically registered BroadcastReceiver
that has a reference to an Activity
so it can modify its UI. I am using Context.registerReceiver()
method but r开发者_StackOverflow中文版eceiver's onReceive()
method is never called.
Here is the sample code showing the problem:
package com.example;
import android.app.Activity;
import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
public class RegisterBroadcastReceiver extends Activity {
public static class MyIntentService extends IntentService {
public MyIntentService() {
super(MyIntentService.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent) {
Intent i = new Intent(this, MyBroadcastReceiver.class);
sendBroadcast(i);
}
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(MyBroadcastReceiver.class.getSimpleName(),
"received broadcast");
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
MyBroadcastReceiver mReceiver = new MyBroadcastReceiver();
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
registerReceiver(mReceiver, intentFilter);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
Intent i = new Intent(this, MyIntentService.class);
startService(i);
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
The whole code if somebody need it.
void alarm(Context context, Calendar calendar) {
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
final String SOME_ACTION = "com.android.mytabs.MytabsActivity.AlarmReceiver";
IntentFilter intentFilter = new IntentFilter(SOME_ACTION);
AlarmReceiver mReceiver = new AlarmReceiver();
context.registerReceiver(mReceiver, intentFilter);
Intent anotherIntent = new Intent(SOME_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, anotherIntent, 0);
alramManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(context, "Added", Toast.LENGTH_LONG).show();
}
class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, "Started", Toast.LENGTH_LONG).show();
}
}
Broadcast receivers receive events of a certain type. I don't think you can invoke them by class name.
First, your IntentFilter must contain an event.
static final String SOME_ACTION = "com.yourcompany.yourapp.SOME_ACTION";
IntentFilter intentFilter = new IntentFilter(SOME_ACTION);
Second, when you send a broadcast, use this same action:
Intent i = new Intent(SOME_ACTION);
sendBroadcast(i);
Third, do you really need MyIntentService to be inline? Static? [EDIT] I discovered that MyIntentSerivce MUST be static if it is inline.
Fourth, is your service declared in the AndroidManifest.xml?
精彩评论