继续服务时,应用程序被杀害

编程入门 行业动态 更新时间:2024-10-18 10:19:10
本文介绍了继续服务时,应用程序被杀害的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个按钮(ON和OFF)的活动:当我点击ON按钮服务启动。当我点击关闭钮该服务停止。现在,我的服务不从我,因为在这种情况下重新启动服务杀从最近的应用的应用程序有问题分开。我不希望它重新启动,但我想它继续工作。该服务是一个START_STICKY。结果这是我的服务code:

I have an activity with two buttons (ON and OFF): when I click the ON button a service starts. When I click the OFF botton the service stops. Now, my service does not have problems apart from when I kill the app from "Recent Apps" because in this circumstance the service restarts. I don't want that it restarts but I want that it continues working. The service is a START_STICKY. This is my "service" code:

@Override public void onCreate() { // TODO Auto-generated method stub myServiceReceiver = new MyServiceReceiver(); mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // Display a notification about us starting. We put an icon in the status bar. showNotification(); } public class LocalBinder extends Binder { SensorService getService() { return SensorService.this; } } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return mBinder; } // This is the object that receives interactions from clients. See // RemoteService for a more complete example. private final IBinder mBinder = new LocalBinder(); @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub if (!running){ IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(MY_ACTION_FROMACTIVITY); registerReceiver(myServiceReceiver, intentFilter); running = true; sensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_NORMAL); } //return super.onStartCommand(intent, flags, startId); return START_STICKY; } @Override public void onDestroy() { Log.v(TAG,"destroy"); // TODO Auto-generated method stub sensorManager.unregisterListener(this); mNM.cancel(NOTIFICATION); this.unregisterReceiver(myServiceReceiver); super.onDestroy(); } private void showNotification() { CharSequence text = getText(R.string.activity); Notification notification = new Notification(R.drawable.lifestyle, text, System.currentTimeMillis()); SharedPreferences flagNot = getSharedPreferences("flagNotification", 0); final SharedPreferences.Editor editor = flagNot.edit(); editor.putBoolean("flagNotification",true); editormit(); Intent notificationIntent = new Intent(this, ActivityTab.class); // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, getText(R.string.app_name),text, contentIntent); notification.flags |= Notification.FLAG_NO_CLEAR; // Send the notification. mNM.notify(NOTIFICATION, notification); } public void onAccuracyChanged(Sensor sensor,int accuracy){ } public void onSensorChanged(SensorEvent event){ ...... } public class MyServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub int hostCmd = arg1.getIntExtra(CMD, 0); if(hostCmd == CMD_STOP){ running = false; stopSelf(); } } }

}

你能帮我吗?结果非常感谢。

Can you help me,please? Many thanks.

推荐答案

在你的服务实施 startForeground ,并发送一个永久通知。

Implement startForeground in your service and send it a persistent notification.

private void startForeground() { int ID = 1234; Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification.Builder builder = new Notification.Builder(getBaseContext()); builder.setContentIntent(pendIntent); builder.setSmallIcon(R.drawable.ic_launcher); builder.setTicker("CUSTOM MESSAGE"); builder.setWhen(System.currentTimeMillis()); builder.setAutoCancel(false); builder.setContentTitle("Test Service"); builder.setContentText("CUSTOM MESSAGE"); Notification notification = builder.build(); startForeground(ID, notification); }

更多推荐

继续服务时,应用程序被杀害

本文发布于:2023-11-27 07:58:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:被杀   应用程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!