我正在开发一个应用程序,该应用程序必须始终运行并且只能通过任务杀手或类似工具关闭。
我在清单中有:
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);
//............
}
我应该在服务标签中添加带有操作的意图过滤器才能正常工作吗?还是其他问题?
问题是无法启动服务。这是修复:
在清单中
<service android:name=".ONService">
<intent-filter android:priority="100">
<action android:name=".ONService"/>
</intent-filter>
</service>
在调用服务的活动中:
Intent serviceIntent = new Intent(".ONService");
startService(serviceIntent);