如果我通过 getBroadcast(someArgs) 创建 PendionIntent,BroadCastReceiver 将不起作用;但是如果我通过 getServie() 创建并在 onStartCommand() 中捕获事件,它工作正常
public class someClass extends Service
{
Notification createNotif()
{
RemoteViews views = new RemoteViews(getPackageName(),R.layout.notif);
ComponentName componentName = new ComponentName(this,someClass.class);
Intent intentClose = new Intent("someAction");
intentClose.setComponent(componentName);
views.setOnClickPendingIntent(R.id.notifClose, PendingIntent.getBroadcast(this, 0, intentClose, PendingIntent.FLAG_UPDATE_CURRENT));
Notification notification = new Notification();
notification.contentView = views;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
return notification;
}
@Override
public void onCreate()
{
super.onCreate();
BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("someAction"))
someMethod();
}
};
IntentFilter intentFilter = new IntentFilter("someAction");
intentFilter.addAction("anyAction");
registerReceiver(broadcastReceiver,intentFilter);
}
}