Android 使用(待定)意图停止服务
Android stop Service using (Pending) Intent
方法我有
private Notification getNotification() {
Activity currentActivity = MyApplication.getApplication().getCurrentActivity();
Intent i = new Intent(currentActivity, SpeechActivationService.class);
i.putExtra(ACTIVATION_STOP_INTENT_KEY, true);
PendingIntent resultPendingIntent = PendingIntent.getActivity(currentActivity, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_mic_off, "Deactivate", resultPendingIntent);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_mic)
.setTicker("TickerSample")
.setWhen(System.currentTimeMillis())
.setContentTitle("My App Voice Control")
.setContentText("Voice Control is Activated")
.addAction(action)
.build();
return notification;
}
它应该发出一个通知,只要服务处于活动状态,该通知就会一直保持活动状态。通知有一个 DEACTIVATE 按钮,它可以发送一个 Intent,包裹在一个 Pending Intent 中(不管有什么区别?!)。似乎 Intents 是通知可以用来处理按钮点击的唯一行动形式,所以我不能只做
stopService(new Intent(this, SpeechActivationService.class));
关于停用。但是当我点击 DEACTIVATE 时,SpeechActivationService 中的代码 onStartCommand() 永远不会被调用。并且服务不继承 onNewIntent() 方法。那么,当我通过按 DEACTIVATE 发送此 Intent 时会调用什么方法?
So what method gets called when I send this intent by pressing DEACTIVATE?
什么都不会。您正在尝试将 getActivity()
PendingIntent
与指向服务的 Intent
结合使用。这将不起作用,您应该在 LogCat 中收到有关该效果的消息。
如果您将 getActivity()
更改为 getService()
,则应调用您的服务 onStartCommand()
。
Pending intents 用于控制应用程序之外的内容(系统功能使用这些内容)。要停止服务,请创建 BroadcastReceiver,它将接收未决意图 (PendingIntent.getBroadcast()),这反过来会停止您的服务
方法我有
private Notification getNotification() {
Activity currentActivity = MyApplication.getApplication().getCurrentActivity();
Intent i = new Intent(currentActivity, SpeechActivationService.class);
i.putExtra(ACTIVATION_STOP_INTENT_KEY, true);
PendingIntent resultPendingIntent = PendingIntent.getActivity(currentActivity, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_mic_off, "Deactivate", resultPendingIntent);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_mic)
.setTicker("TickerSample")
.setWhen(System.currentTimeMillis())
.setContentTitle("My App Voice Control")
.setContentText("Voice Control is Activated")
.addAction(action)
.build();
return notification;
}
它应该发出一个通知,只要服务处于活动状态,该通知就会一直保持活动状态。通知有一个 DEACTIVATE 按钮,它可以发送一个 Intent,包裹在一个 Pending Intent 中(不管有什么区别?!)。似乎 Intents 是通知可以用来处理按钮点击的唯一行动形式,所以我不能只做
stopService(new Intent(this, SpeechActivationService.class));
关于停用。但是当我点击 DEACTIVATE 时,SpeechActivationService 中的代码 onStartCommand() 永远不会被调用。并且服务不继承 onNewIntent() 方法。那么,当我通过按 DEACTIVATE 发送此 Intent 时会调用什么方法?
So what method gets called when I send this intent by pressing DEACTIVATE?
什么都不会。您正在尝试将 getActivity()
PendingIntent
与指向服务的 Intent
结合使用。这将不起作用,您应该在 LogCat 中收到有关该效果的消息。
如果您将 getActivity()
更改为 getService()
,则应调用您的服务 onStartCommand()
。
Pending intents 用于控制应用程序之外的内容(系统功能使用这些内容)。要停止服务,请创建 BroadcastReceiver,它将接收未决意图 (PendingIntent.getBroadcast()),这反过来会停止您的服务