【发布时间】:2017-01-03 21:31:48
【问题描述】:
目前我正试图防止我的服务被应用程序管理器杀死。每次我刷应用程序时,服务也会被杀死。
我尝试过的是 1. 使用 START_STICKY
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand started");
if(!isRunning){
mythread.start();
isRunning = true;
}
return START_STICKY;
}
在此示例中,我的线程每五秒显示一次 toast。
我还创建了一个广播接收器,并给了一个服务单独的进程
<service android:name="com.example.asd.mainhub.GPSTracker"
android:process=":my_process"
android:enabled="true" />
<receiver android:name="com.example.asd.mainhub.BootCompletedIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<action android:name="com.example.asd.mainhub.BootCompletedIntentReceiver" />
</intent-filter>
</receiver>
服务的 onDestroy() 方法,它将使用广播接收器来恢复我的服务,没有工作。
public void onDestroy() {
Intent intent = new Intent("com.example.asd.mainhub.BootCompletedIntentReceiver");
intent.putExtra("yourvalue", "torestore");
sendBroadcast(intent);
}
MainActivity,调用我的服务 onCreate()
gps = new GPSTracker();
mServiceIntent = new Intent(this, gps.getClass());
if (!isMyServiceRunning(gps.getClass())) {
startService(mServiceIntent);
}
我也尝试过使用通知和前台 onCreate() 服务:
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Awesome App")
.setContentText("Doing some work...")
.setContentIntent(pendingIntent).build();
startForeground(1337, notification);
还是一样,每次我交换我的应用程序时,服务也会停止,不再显示 Toast。在我的控制台中,最后的消息是: I/MAINACT:onDestroy! 应用程序终止。 似乎甚至没有调用服务 onDestroy() 方法。请提出一些建议,我该如何解决。
顺便说一句,我将应用程序安装为系统应用程序,它会解决问题吗?
编辑1:
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// // Restore interrupt status.
// Thread.currentThread().interrupt();
// }
while(isRunning){
Log.d(TAG,"Running");
try {
postToastMessage("Toast from Service");
Thread.sleep(DELAY);
} catch (InterruptedException e) {
isRunning = false;
e.printStackTrace();
}
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
//stopSelf(msg.arg1);
}
}
public void postToastMessage(final String message) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
static final long DELAY = 5000;
编辑 2:
HelloService service = new HelloService();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, service.getClass());
if (!isMyServiceRunning(service.getClass())) {
//startService(mServiceIntent);
startService(intent);
}
【问题讨论】:
-
"I'm trying to prevent my service from being killed [...] ",你不能那样做,不可能完成任务 -
你能解释一下为什么吗?尽管我的应用程序打开或关闭,创建系统服务之类的东西是不可能的吗?
-
为什么有很多关于这个主题的文章和教程,人们都说它有效
-
为什么?你想运行一些你无法杀死的恶意服务吗?我不会
-
如果
"alot of articles and tutorials"说有可能,那就关注他们
标签: android android-studio android-service