Android,如何使应用程序的任务无法使用?只能通过任务查杀来关闭

时间:2012-07-31 18:44:25

标签: android taskkill

我开发的应用程序必须始终运行,并且只能与任务杀手或类似程序关闭。

我有清单:

android:persistent="true"

虽然在按下主页按钮时没有关闭,但我发现它不时关闭,我不希望这种情况发生。

我想用户,可以通过从自定义启动器中删除应用程序或使用任务杀手来关闭应用程序。

有什么建议吗?提前谢谢

P.D。:如何使用后退按钮退出应用程序,但不关闭它,我的意思是停止显示应用程序或其任何活动,但应用程序应继续在后台运行。

我写了一个服务,但有些东西工作不正常我在清单

中添加了这个
<service android:name=".ONService"></service>

在应用程序标记结束之前,这是ONService.java:

package com.omninotifier.main;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class ONService extends Service{

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


@Override
public void onCreate() {
    // TODO Auto-generated method stub
    //super.onCreate();
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    //return super.onStartCommand(intent, flags, startId);

    return START_STICKY;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub

    NotificationManager manager = (NotificationManager)     
getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_stat_alert,   
getResources().getString(R.string.ServiceDestroyed), System.currentTimeMillis());
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    manager.notify(150, notification);

    super.onDestroy();
}

 }

我启动服务,正确启动应用程序:

这是应用程序中的主要活动

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


   Intent serviceIntent = new Intent();
   serviceIntent.setAction(".ONService");
   startService(serviceIntent);

//............

}

我应该在服务标签中添加带有操作的intent-filter,以使其正常工作?还是其他问题?

问题在于无法启动该服务。这是修复:

中的

<service android:name=".ONService">
<intent-filter android:priority="100">
    <action android:name=".ONService"/>
</intent-filter>
</service>

在调用服务的活动中:

Intent serviceIntent = new Intent(".ONService");


startService(serviceIntent);

5 个答案:

答案 0 :(得分:2)

  

我正在开发一个必须始终运行的应用程序,并且只能使用任务杀手或类似程序关闭。

这不是严格可能的。 Android可以并且最终将摆脱您的流程,以防止草率开发人员试图拥有“必须始终运行的应用程序”。

如果您的应用程序是前台用户体验的一部分(例如,音乐播放器),您可以使用调用startForeground()的服务,这样可以降低Android消除您的进程的几率,只要有是与您的应用相关联的状态栏中的Notification。你的应用程序最终仍然会终止其进程,但它往往需要更长的时间。

或者,您可以自己定制构建包含实现逻辑的C守护程序的Android操作系统,并分发包含自定义Android的ROM模块。

答案 1 :(得分:1)

要让您的应用在关闭后继续运行,您需要在应用内部提供服务,以便在后台执行任何操作。

如果您不熟悉开发Android应用,可能需要查看以下Android服务教程:http://www.vogella.com/articles/AndroidServices/article.html。另外,我发现Android文档非常有用。我建议您同时阅读API Guide for ServicesAPI Reference for the Service class

答案 2 :(得分:1)

写一项服务。您的应用与服务进行通信。当您的应用退出时,该服务仍在运行。

http://developer.android.com/reference/android/app/Service.html

答案 3 :(得分:0)

使用Service,AlarmManager和BroadcastReceiver的组合。 AlarmManager发出一个脉冲,BCReceiver会根据需要随时捕获并启动服务。这将基本上使服务保持活力,因为Android会看到它会定期使用。从技术上讲,你的应用程序的生命周期,服务是在机器人控制下,但你可以启动服务粘性,这有助于并定期调用它。 AlarmManager非常可靠。

答案 4 :(得分:0)

如果您需要随时投放,请查看ServicestartForeground。如果您可以让服务消失但重新启动,请查看onStartCommandSTART_STICKY

相关问题