Inner Broadcast receiver does not work?
I create an inner receiver InnerReceiver
public class MainActi开发者_如何学运维vity extends Activity{
...
class InnerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("InnerReceiver", "onReceive");
}
}
...
}
The receiver is called as following:
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, new Intent(context,MainActivity.InnerReceiver.class), 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+1000,
10000,
pi);
The inner class is declared as:
However, the inner receiver is never called! Is anything wrong ? Thanks.
You need to register it in the <receiver>
tag in your AndroidManifest.xml or dynamically by calling registerReceiver()
.
It may need to be declared public
, rather than package-private
as you have it. See more about Java access control here.
You will also need to broadcast something for it to receive, such as by calling sendBroadcast()
.
精彩评论