【发布时间】:2016-05-20 07:59:00
【问题描述】:
我想在同一个服务类中接收 PendingIntent。 我在 onReceive 方法中添加了一个日志,但它从未在 LogCat 中显示。
AndroidManifest:
<service android:name=".UpdateService" android:exported="true">
<intent-filter>
<action android:name="monitoringStop" />
</intent-filter>
</service>`
更新服务:
public class UpdateService extends Service {
public static final String BROADCAST_NAME = "monitoringStop";
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Never reached
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//registering
IntentFilter filter = new IntentFilter();
filter.addAction(UpdateService.BROADCAST_NAME);
registerReceiver(receiver, filter);
//creating Intent + PendingIntent
Intent stopIntent = new Intent(this, UpdateService.class);
stopIntent.setAction(BROADCAST_NAME);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, 1, stopIntent, PendingIntent.FLAG_ONE_SHOT);
//adding PendingIntent to action
NotificationCompat.Action stopService
= new NotificationCompat.Action(R.drawable.stop, getString(R.string.stop), pendingIntent);
NotificationCompat.Builder notificationBuilder
= new NotificationCompat.Builder(this)//settingText etc.
.addAction(stopService);
startForeground(1,notificationBuilder.build());
return Service.START_STICKY;
}
}
【问题讨论】:
-
你在哪里发送广播?,发布代码
-
倒数第三行:
.addAction(stopService);link
标签: android android-intent android-pendingintent