Android,如何使应用程序的任务不可关闭?只能通过任务关闭被关闭
我正在开发一个必须始终运行并且只能用任务杀手或类似程序关闭的应用程序。Android,如何使应用程序的任务不可关闭?只能通过任务关闭被关闭
我在清单:
android:persistent="true"
虽然按home键时不打烊,我觉得从时间关闭时间,我不希望这种事情发生。
我想要的用户,可以通过从一个自定义启动杀死它,或者使用任务的杀手关闭App。
有什么建议吗?在此先感谢
Pd积:你怎么能离开与返回按钮的应用程序,但不关闭它,我的意思是停止显示该应用程序或任何活动,但应用程序应该继续在后台运行。
我写了一个服务,但是有什么不工作罚款我加入这个清单中的
<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);
//............
}
我要补充意向滤波器这个工作在服务标记操作?或者是其他问题?
的问题是,它无法启动该服务。这是修复:
在清单
<service android:name=".ONService">
<intent-filter android:priority="100">
<action android:name=".ONService"/>
</intent-filter>
</service>
在调用该服务的活动
:
Intent serviceIntent = new Intent(".ONService");
startService(serviceIntent);
写一个服务。您的应用程序与服务进行通信。当您的应用退出时,该服务仍在运行。
http://developer.android.com/reference/android/app/Service.html
我对android中的服务一无所知,我会看到它,如果它能正常工作,我会将您的答案设置为我接受的答案,谢谢! – 2012-08-01 09:22:48
应用程序活动和服务具有不同的生活范围。在服务中使用startForeground()作为您的第二个答案,其长寿命 – Nepster 2015-03-18 07:30:48
让自己的应用继续运行在关闭后,你需要做一个服务在你的应用程序内部做你想在后台发生的事情。
如果你是新开发Android应用程序,你可能想看看下面的教程Android服务:http://www.vogella.com/articles/AndroidServices/article.html。另外,我发现Android文档非常有帮助。我建议你阅读API Guide for Services和API Reference for the Service class。
我正在开发的是必须一直运行,并只与一个任务的杀手或类似的可关闭的应用程序。
这不是严格可行的。Android可以也将最终摆脱您的流程,防止漫不经心的开发者尝试拥有“必须始终运行的应用程序”。
如果您的应用程序是前台用户体验的一部分(例如音乐播放器),则可以使用调用startForeground()
的服务,这样可以降低Android摆脱进程的几率,只要有Notification
在与您的应用关联的状态栏中。你的应用程序最终还是会终止它的进程,尽管它会花费更长的时间。或者,您可以制作自己的Android操作系统自定义版本,其中包含一个实现您的逻辑的C守护进程,并分发包含您的自定义Android的ROM模组。
+1。大声笑 – Dediqated 2013-05-22 12:31:56
使用Service,AlarmManager和BroadcastReceiver的组合。 AlarmManager发出脉冲,BCReceiver根据需要频繁地捕获和启动服务。这将基本上保持服务无休止的活动,因为Android会看到它定期被使用。从技术上讲,你的应用程序,服务的lifecyce是在androids控制下,但你可以启动服务粘性,这有助于并定期调用它。 AlarmManager非常可靠。
应用程序实际上从不打开或关闭。这是一种幻觉,只有Android正在运行,它会根据自己的算法路由到活动和服务,但是要实现您想要的效果,请参阅下面的答案。 – 2012-07-31 20:02:26