仅此标志不会停止服务。我建议你制定停止行动,而不是发射一个自定义BroadcastReceiver
类,该类在其onReceive()
内运行stopService()
方法。让我知道你是否需要帮助设置更详细的内容。
编辑答案:
更改Intent
和PendingIntent
的隐藏行动,这一点:
Intent intentHide = new Intent(this, StopServiceReceiver.class);
PendingIntent hide = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), intentHide, PendingIntent.FLAG_CANCEL_CURRENT);
然后使StopServiceReceiver
这个样子,哪里ServiceYouWantStopped.class
是要停止的服务:
public class StopServiceReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 333;
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, ServiceYouWantStopped.class);
context.stopService(service);
}
}
确保您刚刚制作的BroadcastReceiver
已在您的清单文件中声明e:
<receiver
android:name=".StopServiceReceiver"
android:enabled="true"
android:process=":remote" />
希望这有助于!
可能重复的[PendingIntent启动和停止服务](http://stackoverflow.com/questions/20572216/pendingintent-to-launch-and-stop-a-service) –
为什么你使用相同的请求两种不同行为的代码?我认为你与PendingIntent标志混淆。这些标志用于控制通知。 – Ayaanp