Android 如何从自己的前台通知停止服务
我有一个Android 如何从自己的前台通知停止服务,android,service,notifications,kill,Android,Service,Notifications,Kill,我有一个服务正在运行。在它的onStartCommand中,我正在执行startforeground以避免被系统杀死 public int onStartCommand(Intent intent, int flags, int startId) { if (ACTION_STOP_SERVICE.equals(intent.getAction())) { Log.d(TAG,"called to cancel service"); manager.can
服务
正在运行。在它的onStartCommand
中,我正在执行startforeground
以避免被系统杀死
public int onStartCommand(Intent intent, int flags, int startId) {
if (ACTION_STOP_SERVICE.equals(intent.getAction())) {
Log.d(TAG,"called to cancel service");
manager.cancel(NOTIFCATION_ID);
stopSelf();
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("abc");
builder.setContentText("Press below button to stoP.");
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setSmallIcon(R.drawable.ic_launcher);
Intent stopSelf = new Intent(this, SameService.class);
stopSelf.setAction(this.ACTION_STOP_SERVICE);
PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,0);
builder.addAction(R.drawable.ic_launcher, "Stop", pStopSelf);
manager.notify(NOTIFCATION_ID, builder.build());
}
但按下按钮后,pendingent
不起作用,我的活动也不会因此而停止
有人能告诉我,我在这里做错了什么,或者有什么其他的解决方案来停止前台的服务吗
为了其他像我这样的发现者,谢谢你回答我自己的问题
问题在下面这行
PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,0);
这0最终是问题的原因。
我已经用PendingEvent.FLAG\u CANCEL\u CURRENT替换了它,现在它工作了
更正的代码为:
PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,PendingIntent.FLAG_CANCEL_CURRENT);
请查看更多解释。上述想法无法正常工作。服务应该首先停止它的线程,所以有时候你会看到奇怪的行为。您应该在循环/计算方法中添加一些标志并调用“return;
”,然后您可以通过stopself()
停止服务,或者等待它自己完成。如果需要的话,我可以举个例子。因此,请询问。如果您正在使用该服务,则该服务无法自行停止其在后台的运行,您必须停止该操作。在此代码中,我将停止通知时的服务按钮单击它为我工作。。请试试这个
public class AlarmSoundService extends Service {
public static final int NOTIFICATION_ID = 1;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
if (intent != null) {
if (intent.getAction().equals(Constants.ACTION_START)) {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
Date date = new Date();
List<Event> list = SharedPref.getInstance(getApplicationContext()).getEvents();
for (int i = 0; i < list.size(); i++) {
Event a = list.get(i);
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy", Locale.getDefault());
String currentDate = format.format(date);
if (a.getDate().equals(currentDate)) {
date = new Date();
format = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
if (a.getTime().equals(format.format(date))) {
playAlarmNotification(a.getTitle(), a.getDescription());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 1000);
} else if (intent.getAction().equals(Constants.ACTION_STOP)) {
stopForegroundService();
}
}
return START_STICKY;
}
public void playAlarmNotification(String Title, String Description) {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent stopnotificationIntent = new Intent(this, AlarmSoundService.class);
stopnotificationIntent.setAction(Constants.ACTION_STOP);
PendingIntent Intent = PendingIntent.getService(this, 0, stopnotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.ic_access_time_black_24dp)
.setContentTitle(Title)
.setContentText(Description)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(Color.BLUE)
.setDefaults(Notification.DEFAULT_ALL)
.setFullScreenIntent(pendingIntent, true)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_media_pause, "Stop", Intent);
Notification notification = builder.build();
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel("channel_id", "background_service", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("hello");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
startForeground(NOTIFICATION_ID, notification);
}
private void stopForegroundService() {
stopForeground(true);
stopSelf();
}
}
公共类AlarmSoundService扩展服务{
公共静态最终整数通知_ID=1;
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共int onStartCommand(最终意图、int标志、int startId){
if(intent!=null){
if(intent.getAction().equals(Constants.ACTION_START)){
最终处理程序=新处理程序();
定时器=新定时器();
TimerTask doAsynchronousTask=新TimerTask(){
@凌驾
公开募捐{
handler.post(新的Runnable(){
公开募捐{
试一试{
日期=新日期();
List List=SharedPref.getInstance(getApplicationContext()).getEvents();
对于(int i=0;i=26){
NotificationChannel=new NotificationChannel(“频道id”、“后台服务”、NotificationManager.IMPORTANCE\u默认值);
channel.setDescription(“你好”);
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
notificationManager.createNotificationChannel(频道);
}
startForeground(通知ID,通知);
}
私有void stopForegroundService(){
停止前景(真);
stopSelf();
}
}
我想举个例子