简体   繁体   中英

start a service automatically if ram is cleared by user in android

How to start a service automatically if Ram is cleared by user in android I have created one broadcastReciever and starting the service Sorry if I am not very cleared ..some time it is working means if user clearing ram then still service is running but some times it is stopped and its not restarting. how to solve this problem

2) I have used alarm manager but application getting hanged frequently so I quit alarm manager My code snippet are

public class RecieverToRestartService extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try 
{

            Intent  _restartServiceIntent = new Intent(context,InvendisGpsLoggerActivity.class);
            _restartServiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(_restartServiceIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

and in android manifest file

<receiver   android:name="com.i.I.RecieverToRestartService">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REPLACED"/>
                <action android:name="android.intent.action.PACKAGE_RESTARTED"/>     
            </intent-filter>
        </receiver>

在您的情况下,我还没有测试过,但是...当您在public int onStartCommand (Intent intent, int flags, int startId)返回START_STICKY public int onStartCommand (Intent intent, int flags, int startId)时,如果您的手机由于public int onStartCommand (Intent intent, int flags, int startId)过低而将其杀死,则会在一段时间后自动重新启动该服务记忆。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM