机器人;启动后服务自动启动:FLAG_INCLUDE_STOPPED_PACKAGES无法正常工作

时间:2015-07-20 10:03:09

标签: android

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

这是我的代码。

的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;
    }
}

2 个答案:

答案 0 :(得分:4)

您不应将Sub dural() x = 1.23456789 With ActiveCell .Value = x .NumberFormat = "[hh]:mm:ss" End With End Sub 添加到您的接收方启动服务的意图中。您必须将其添加到用于FLAG_INCLUDE_STOPPED_PACKAGES的意图中。意思是,您需要将其添加到调用Broadcast的应用程序中的intent。 这就是为什么这个标志与你的代码无关。

如果你只是从你的申请表外sendBroadcast到这个接收者(&#34; Salva_autostart&#34;),那么你的申请将不会在#34;强制停止&#34;再次声明并在下次启动时触发你的接收器。

此外,您应该添加(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); 中,或者与<intent-filter>操作中已有的相同。

BOOT_COMPLETED

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

答案 1 :(得分:0)

请注意,从Android 3.0开始,用户需要在应用程序收到android.intent.action.BOOT_COMPLETED事件之前至少启动一次应用程序。