Android, How to make the task of the app unclosable? Only closable by task killing

╄→гoц情女王★ 提交于 2019-12-06 13:02:09

问题


I'm developing an app that have to be always running and be only closable with a task killer or similar.

I have in the manifest:

android:persistent="true"

Although is not closing when pressing home button, I find it closed from time to time and I don't want this to happen.

I user want, can close app by killing it from a custom launcher or using task killer.

Any suggestions? thanks in advance

P.D.: how can you exit the app with the back button, but not closing it, I mean stop showing the app or any of its activities but the app should continue running in background.

I wrote a service but something is not working fine I added this in the manifest

<service android:name=".ONService"></service>

right before the end of application tag, and this is the 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();
}

 }

and I start the service doing this right the app starts:

this is the main activity in the app

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


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

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

}

I should add intent-filter with actions in the service tag for this to work? or it is other issue?

The problem was that it was unable to start the service. This is the fix:

in Manifest

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

in the activity that calls the service:

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


startService(serviceIntent);

回答1:


Write a service. Your app communicates with the service. When your app exits, the service remains running.

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




回答2:


I'm developing an app that have to be always running and be only closable with a task killer or similar.

That is not strictly possible. Android can and will eventually get rid of your process, to prevent sloppy developers from trying to have "an app that have to be always running".

If your application is part of the foreground user experience (e.g., music player), you can have a service that calls startForeground(), which reduces the odds that Android will get rid of your process, so long as there is a Notification in the status bar associated with your app. Your app still will get its process terminated eventually, though it will tend to take longer.

Or, you can make your own custom build of the Android operating system that contains a C daemon that implements your logic, and distribute a ROM mod containing your customized Android.




回答3:


To have your app continue running after you close it, you need to make a service inside you app to do whatever you want to happen in the background.

If you are new to developing Android apps, you might want to take a look at the following tutorial for Android services: http://www.vogella.com/articles/AndroidServices/article.html. In addition, I have found that the Android documentation is very helpful. I would suggest you read both the API Guide for Services and the API Reference for the Service class.




回答4:


Use a combination of Service, AlarmManager, and BroadcastReceiver. The AlarmManager issues a pulse, the BCReceiver catches and starts the service every so often as needed. This will essentially keep the service alive endlessly since Android will see that it periodically gets used. Technically the lifecylce of your app, service is under androids control, but you can start the service sticky, which helps and periodically call it. AlarmManager is extremely reliable.




回答5:


If you need to run at all times, look into a Service and startForeground. If you can let your Service die but get restarted, look into onStartCommand and START_STICKY.



来源:https://stackoverflow.com/questions/11746875/android-how-to-make-the-task-of-the-app-unclosable-only-closable-by-task-killi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!