我接收小部件发送的意图为PendingIntent:
时遇到问题intent = new Intent(MyService.MY_ACTION);
pendingIntent = PendingIntent.getService(this, 0, intent, 0);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
我在MyService中添加了一个广播接收器:
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "Intent command received");
String action = intent.getAction();
if( MY_ACTION.equals(action))
{
doSomeAction();
}
}
};
最后我注册了这个接收器i onCreate服务方法:
IntentFilter filter = new IntentFilter();
filter.addAction(MY_ACTION);
registerReceiver(mIntentReceiver, filter);
现在当MyService运行时我单击按钮,我得到:
09-21 14:21:18.723: WARN/ActivityManager(59): Unable to start service Intent { act=com.myapp.MyService.MY_ACTION flg=0x10000000 bnds=[31,280][71,317] }: not found
我还尝试将一个intent-filter(带有MY_ACTION操作)添加到MyService的清单文件中,但它导致调用MyService的onStartCommand方法。这不是我想要的。我需要调用mIntentReceiver的onReceive方法。
请你帮我解决这个问题。谢谢!
答案 0 :(得分:3)
你想做什么?
如果您在服务onCreate
方法中注册广播接收器,则:
您应该提出广播意图,而不是服务意图。改变这个:
pendingIntent = PendingIntent.getService(this,0,intent,0);
对此:
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
答案 1 :(得分:1)
出于此目的,您应该从IntentService
扩展您的服务。您将通过onHandleIntent()
服务方式收到广播。