【问题标题】:android; service autostarting after boot: FLAG_INCLUDE_STOPPED_PACKAGES not working安卓;启动后服务自动启动:FLAG_INCLUDE_STOPPED_PACKAGES 不起作用
【发布时间】:2015-07-20 10:03:09
【问题描述】:

我已阅读有关此问题的几个答案,但发布的解决方案对我不起作用。我的代码中可能有一些错误或遗漏。 我需要我的应用程序在启动完成后自动启动,没有任何活动。 如果我包含一个活动,只是为了第一次启动应用程序(退出停止状态),一切正常。 提前感谢您的帮助。

这是我的代码。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zag.salva" >

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver
        android:name=".Salva_autostart"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service
        android:name=".Salva_servizio"
        android:enabled="true" >
        <intent-filter>
            <action android:name=".Salva_servizio" />
        </intent-filter>
    </service>

</application>

Salva_autostart.java

public class Salva_autostart extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent intento = new Intent(context, Salva_servizio.class);
        intento.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        context.startService(intento);
    }


}

Salva_servizio.java

public class Salva_servizio extends Service
{
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        // Task execution
        Salva_invio2 invio = new Salva_invio2();
        invio.esegui(this);
        return Service.START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent)
    {
       return null;
    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    您不应将FLAG_INCLUDE_STOPPED_PACKAGES 添加到接收者启动服务的意图中。您必须将其添加到您用于sendBroadcast 的意图中。这意味着,您需要将其添加到调用广播的应用程序的意图中。 这就是为什么这个标志在你的代码中是不相关的。

    如果您只从您的应用程序外部向此接收器(“Salva_autostart”)sendBroadcast 一次 - 那么您的应用程序将不再处于“强制停止”状态,并且在下次启动时您的接收器将被触发。

    您还应该添加 (addFlags) 而不是设置 (setFlags)。

     intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    

    这就是你应该如何从另一个应用程序触发你的接收器:

       Intent intent = new Intent("com.xxx.my_filter_intent");
       intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
       this.sendBroadcast(intent);
    

    在您的清单中,将上述过滤器意图添加到您的接收器(您可以将其添加到新的 &lt;intent-filter&gt; 或您已经为 BOOT_COMPLETED 操作添加的相同中。

    <receiver
        android:name=".Salva_autostart"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
    
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.xxx.my_filter_intent" />
        </intent-filter>
    </receiver>
    

    在这里阅读更多: http://developer.android.com/about/versions/android-3.1.html#launchcontrols

    【讨论】:

    • 即使在从设备设置中强制关闭应用程序后,它是否仍让我的服务在后台运行?
    • Hi HimCream,它不会在强制停止后保持服务运行,在广播中使用此标志只会在再次发送广播时触发您的接收器(即使您的应用程序已强制停止 atate )。然后,如果您的接收器启动服务 - 您的服务将再次启动。
    【解决方案2】:

    请注意,从 Android 3.0 开始,用户需要至少启动一次应用程序,然后您的应用程序才能接收 android.intent.action.BOOT_COMPLETED 事件。

    【讨论】:

    • 但这将是默认行为;这就是我介绍 setFlags() 语句的原因;不是这样的吗?谢谢
    • 应用程序在首次安装但尚未启动以及由用户手动停止时(在管理应用程序中)处于停止状态。默认情况下,系统将 FLAG_EXCLUDE_STOPPED_PACKAGES 添加到所有广播意图中,并且您的 BroadcastReceiver 正在侦听系统广播。因此,如果用户不启动您的应用程序,基本上您的 Salva_autostart 将不会获得任何 Intent。如果它没有得到任何 Intent,它也无法启动 Service。
    • 你还在想吗?我的回答对吗?如果是这样,请评分或投票。
    • 显然它正在启动 Android 3.1 stackoverflow.com/a/19856267/1630447
    猜你喜欢
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多