Java 在持久通知中实现退出应用程序按钮
我的应用程序运行由持久通知指示的后台服务。用户需要使用MainActivity中的切换按钮来打开/关闭此服务,从而删除持久性通知 现在我想实现一个Java 在持久通知中实现退出应用程序按钮,java,android,service,android-service,android-notifications,Java,Android,Service,Android Service,Android Notifications,我的应用程序运行由持久通知指示的后台服务。用户需要使用MainActivity中的切换按钮来打开/关闭此服务,从而删除持久性通知 现在我想实现一个通知操作,它可以关闭此服务&以及MainActivity中的切换。总之,它应该是一个退出按钮,可以直接从通知中关闭我的应用程序和后台服务 我如何做到这一点 注意,我有两个不同的java文件,一个用于NotificationService 另一个是主要活动。切换属于MainActivity 编辑:可以调用System.exit(0)吗?如果我将挂起的意图
通知操作
,它可以关闭此服务&以及MainActivity中的切换。总之,它应该是一个退出按钮,可以直接从通知中关闭我的应用程序和后台服务
我如何做到这一点
注意,我有两个不同的java文件,一个用于NotificationService
另一个是主要活动。切换属于MainActivity
编辑:可以调用System.exit(0)吗?如果我将挂起的意图用于 BroadCastReceiver是否完全退出应用程序
您必须将PendingEvent与BroadcastReceiver或Service一起使用才能执行此操作。下面是一个带有广播接收器的PendingEvent示例 生成通知
public static void createNotif(Context context){
Intent intentAction = new Intent(context,StopServerBroadcast.class);
//This is optional if you have more than one buttons and want to differentiate between two
intentAction.putExtra("action","actionName");
pIntent = PendingIntent.getBroadcast(context,1,intentAction,PendingIntent.FLAG_UPDATE_CURRENT);
drivingNotifBldr = (NotificationCompat.Builder) new NotificationCompat.Builder(context, CHANNEL_NAME)
.setSmallIcon(R.drawable.steeringwheel)
.setContentTitle("Stop Service")
.setContentText("Example Text")
.addAction(R.drawable.smallmanwalking, "On/off", pIntent)
.setOngoing(true);
...
}
现在是接收此意向的接收者
public class StopServerBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context,"recieved",Toast.LENGTH_SHORT).show();
String action=intent.getStringExtra("action");
if(action.equals("action1")){
performAction1();
/*
Code that will stop your service
*/
}
}
}
<receiver
android:name=".StopServerBroadcast"
android:enabled="true" />
在清单中注册接收人
public class StopServerBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context,"recieved",Toast.LENGTH_SHORT).show();
String action=intent.getStringExtra("action");
if(action.equals("action1")){
performAction1();
/*
Code that will stop your service
*/
}
}
}
<receiver
android:name=".StopServerBroadcast"
android:enabled="true" />
您必须将PendingEvent与BroadcastReceiver或Service一起使用才能执行此操作。下面是一个带有广播接收器的PendingEvent示例 生成通知
public static void createNotif(Context context){
Intent intentAction = new Intent(context,StopServerBroadcast.class);
//This is optional if you have more than one buttons and want to differentiate between two
intentAction.putExtra("action","actionName");
pIntent = PendingIntent.getBroadcast(context,1,intentAction,PendingIntent.FLAG_UPDATE_CURRENT);
drivingNotifBldr = (NotificationCompat.Builder) new NotificationCompat.Builder(context, CHANNEL_NAME)
.setSmallIcon(R.drawable.steeringwheel)
.setContentTitle("Stop Service")
.setContentText("Example Text")
.addAction(R.drawable.smallmanwalking, "On/off", pIntent)
.setOngoing(true);
...
}
现在是接收此意向的接收者
public class StopServerBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context,"recieved",Toast.LENGTH_SHORT).show();
String action=intent.getStringExtra("action");
if(action.equals("action1")){
performAction1();
/*
Code that will stop your service
*/
}
}
}
<receiver
android:name=".StopServerBroadcast"
android:enabled="true" />
在清单中注册接收人
public class StopServerBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context,"recieved",Toast.LENGTH_SHORT).show();
String action=intent.getStringExtra("action");
if(action.equals("action1")){
performAction1();
/*
Code that will stop your service
*/
}
}
}
<receiver
android:name=".StopServerBroadcast"
android:enabled="true" />
好的,这似乎很有希望。但我仍然有一个问题,因为开/关切换在MainActivity中,如果我使用BroadcastReceiver创建这个新类,那么我如何从这个类访问该切换?调用System.exit(0)可以吗或者这违反了play store策略?使一个静态布尔变量isActive=true或false,并在后台服务中继续检查其true或false,如果brodcast Receiver关闭,则停止服务器并验证该变量这是genius!谢谢你,兄弟,你救了我一天。好吧,这看起来很有希望。但我仍然有一个问题,因为开/关切换在MainActivity中,如果我使用BroadcastReceiver创建这个新类,那么我如何从这个类访问该切换?调用System.exit(0)可以吗或者这违反了play store策略?使一个静态布尔变量isActive=true或false,并在后台服务中继续检查其true或false,如果brodcast Receiver关闭,则停止服务器并验证该变量这是genius!谢谢你,兄弟,你救了我一天。