Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在打开应用程序后后台服务停止,新服务启动_Android_Service - Fatal编程技术网

Android在打开应用程序后后台服务停止,新服务启动

Android在打开应用程序后后台服务停止,新服务启动,android,service,Android,Service,我创建了一个在手机启动时启动的服务。但当我打开应用程序时,服务会再次启动,旧的运行服务会停止,但服务会工作。但当我关闭应用程序时,该服务也会停止。如何使用启动时启动的服务,如果启动时启动的服务被系统终止,如何重新运行该服务 这是我的密码 AndroidManifest.xml <receiver android:name=".MyBroadcastReceiver" > <intent-filter> <action android:na

我创建了一个在手机启动时启动的服务。但当我打开应用程序时,服务会再次启动,旧的运行服务会停止,但服务会工作。但当我关闭应用程序时,该服务也会停止。如何使用启动时启动的服务,如果启动时启动的服务被系统终止,如何重新运行该服务

这是我的密码

AndroidManifest.xml

<receiver android:name=".MyBroadcastReceiver" >
     <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
     </intent-filter>
</receiver>

<service android:name=".AppMainService" />
AppMain服务

public class AppMainService extends IntentService {

    private Timer timer;
    private ReceiveMessagesTimerTask myTimerTask;
    public static AppPreferences _appPrefs;
    public static SQLiteDatabase qdb;
    public static Config config;
    public static Engine engine;

    /**
     * A constructor is required, and must call the super IntentService(String)
     * constructor with a name for the worker thread.
     */
    public AppMainService() {
        super("HelloIntentService");
    }
    public void onStart(Intent intent, Integer integer) {
        super.onStart(intent, integer);
    }
    public void onCreate() {
        super.onCreate();
        DB db = new DB(this);
        qdb = db.getReadableDatabase();

        _appPrefs = new AppPreferences(getApplicationContext());
        config = new Config();
        engine = new Engine();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, startId, startId);
        Log.i("sss", "Service sarted");
        return START_REDELIVER_INTENT;
    }
    /**
     * The IntentService calls this method from the default worker thread with
     * the intent that started the service. When this method returns, IntentService
     * stops the service, as appropriate.
     */
    @Override
    protected void onHandleIntent(Intent intent) {
        timer = new Timer();
        myTimerTask = new ReceiveMessagesTimerTask();
        timer.schedule(myTimerTask, 0, 10000);
    }

    class ReceiveMessagesTimerTask extends TimerTask {

        @Override
        public void run() {
            //Sending messages
            Log.i("Service", String.valueOf(System.currentTimeMillis())+_appPrefs.getToken());
        }
    }
}
在我的活动中

protected void onCreate(Bundle savedInstanceState) {
        ...

        Intent intent = new Intent(this, AppMainService.class);
        startService(intent);
}

这种行为是经过设计的,因为您正在子类化IntentService。一旦处理完所有意图,它就会自动关闭。若您希望您的服务持久化,那个么可以扩展服务,并实现您自己的线程机制

protected void onCreate(Bundle savedInstanceState) {
        ...

        Intent intent = new Intent(this, AppMainService.class);
        startService(intent);
}