gpt4 book ai didi

安卓;启动后服务自动启动 : FLAG_INCLUDE_STOPPED_PACKAGES not working

转载 作者:行者123 更新时间:2023-11-29 15:48:51 25 4
gpt4 key购买 nike

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

这是我的代码。

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

最佳答案

你不应该添加 FLAG_INCLUDE_STOPPED_PACKAGES按照您的接收者的 Intent 开始您的服务。您必须将其添加到用于 sendBroadcast 的 Intent 中.意思是,您需要将它添加到调用广播的应用程序的 Intent 中。这就是此标志与您的代码无关的原因。

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

您还应该添加 ( 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);

在您的 list 上,将上述过滤器 Intent 添加到您的接收器(您可以将其添加到新的 <intent-filter> 中,或者添加到您已有的 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

关于安卓;启动后服务自动启动 : FLAG_INCLUDE_STOPPED_PACKAGES not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31513663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com