如何在android中单击强制停止时运行服务

如何在android中单击强制停止时运行服务,第1张

概述单击强制停止时我遇到了运行服务的问题,当我重启我的手机时应该调用服务.我已经按照一些例子但我无法完成任务.任何人都可以指导我完成任务. 需要: 1.Service should run when force stop has been clicked from settings2.Service should run when mobile has been restarted. TestAct 单击强制停止时我遇到了运行服务的问题,当我重启我的手机时应该调用服务.我已经按照一些例子但我无法完成任务.任何人都可以指导我完成任务.

需要:

1.Service should run when force stop has been clicked from settings2.Service should run when mobile has been restarted.

TestActivity.java

包com.testsearching;

import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;public class TestActivity extends Activity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_search);        startService(new Intent(this,ServiceTest.class));    }}

ServiceTest.java

package com.testsearching;import java.util.Timer;import java.util.TimerTask;import androID.app.Service;import androID.content.Intent;import androID.os.IBinder;import androID.util.Log;import androID.Widget.Toast;public class ServiceTest extends Service {    @OverrIDe    public IBinder onBind(Intent intent) {        return null;    }    @OverrIDe    public voID onCreate() {        super.onCreate();        mTimer = new Timer();        mTimer.schedule(timerTask,2000,2 * 1000);    }    @OverrIDe    public int onStartCommand(Intent intent,int flags,int startID) {        try {        } catch (Exception e) {            e.printstacktrace();        }        return super.onStartCommand(intent,flags,startID);    }    private Timer mTimer;    TimerTask timerTask = new TimerTask() {        @OverrIDe        public voID run() {            Log.e("Log","Running");        }    };    public voID onDestroy() {        try {            mTimer.cancel();            timerTask.cancel();        } catch (Exception e) {            e.printstacktrace();        }        Intent intent = new Intent("com.androID.techtrainner");        intent.putExtra("yourvalue","torestore");        sendbroadcast(intent);    }}

ReceiverCall.java

package com.testsearching;    import androID.content.broadcastReceiver;    import androID.content.Context;    import androID.content.Intent;    import androID.util.Log;    import androID.Widget.Toast;    public class ReceiverCall extends broadcastReceiver {        @OverrIDe        public voID onReceive(Context context,Intent intent) {            if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {            Log.i("Service Stops","Ohhhhhhh");            context.startService(new Intent(context,ServiceTest.class));;            Toast.makeText(context,"My start",Toast.LENGTH_LONG).show();            }        }}

的Manifest.xml

<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"    package="com.testsearching"    androID:versionCode="1"    androID:versionname="1.0" >    <uses-sdk        androID:minSdkVersion="14"        androID:targetSdkVersion="16" />    <uses-permission androID:name="androID.permission.MODIFY_AUdio_SETTINGS" />    <uses-permission androID:name="androID.permission.SEND_SMS" />    <uses-permission androID:name="androID.permission.PROCESS_OUTGOING_CALLS" />    <uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED" />    <application        androID:allowBackup="true"        androID:theme="@style/Apptheme" >        <activity            androID:name="com.testsearching.TestActivity"            androID:label="@string/app_name"            androID:theme="@androID:style/theme.No@R_301_5979@bar" >            <intent-filter>                <action androID:name="androID.intent.action.MAIN" />                <category androID:name="androID.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service androID:name=".ServiceTest" >            <intent-filter>        <action androID:name="com.testsearching.ServiceTest" />    </intent-filter>        </service>        <receiver            androID:name="ReceiverCall"            androID:enabled="true"            androID:exported="true"            androID:permission="androID.permission.RECEIVE_BOOT_COMPLETED" >            <intent-filter>                <action androID:name="com.androID.techtrainner" />                <action androID:name="androID.intent.action.SCREEN_ON" />                <action androID:name="androID.intent.action.BOOT_COMPLETED" />            </intent-filter>        </receiver>    </application></manifest>
解决方法 从理论上讲,这是不可能的;根据AndroID安全模型.

正如Panaj Kumar在评论中指出的那样:

When user does force stop,means he does not want to run this
application (any component). he is not interested anymore,and this
is rights of user. SO androID does not gives a way to keep running
your service,even after forced close your app.

AndroID将阻止应用程序使用START_STICKY标志重新启动,并将禁用RECEIVE_BOOT_COMPLETED接收器.系统还将禁用为此应用程序设置的所有警报.

在系统允许应用程序再次运行之前,用户必须自己运行应用程序的活动.

也就是说,似乎某些应用仍然能够以这种方式打破规则.这应该被认为是不正确的并且将利用安全漏洞,但是它表明它仍然可能,即使在KitKat上也是如此.

discovery Insure驱动应用程序似乎能够在强制停止时重新启动,并将在启动时重新启动:

> Discovery Insure Driving Challenge on Play Store

但是,不应依赖此功能 – 希望此安全漏洞将在未来的系统更新中得到修复.

总结

以上是内存溢出为你收集整理的如何在android中单击强制停止时运行服务全部内容,希望文章能够帮你解决如何在android中单击强制停止时运行服务所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1125290.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存