gpt4 book ai didi

android - BOOT_COMPLETED 无法正常工作

转载 作者:IT王子 更新时间:2023-10-29 00:05:13 27 4
gpt4 key购买 nike

首先,我知道已经有数百个此类问题被提出,但我已经检查了一段时间,仍然找不到任何解决方案。

我见过 this answer说 BOOT_COMPLETED 不会发送到应用程序,除非用户在 Android 版本 3.1 之后首先启动您的应用程序但我仍然看到一些应用程序正在这样做,一定有办法。我确实需要处理它,否则我也反对在没有用户交互的情况下做某事。

这是我的 AndroidManifest:

<manifest ... >

<!-- to be activated service on boot is completed -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application ... >
<!-- to receive data when boot completed -->
<receiver
android:name="myPackage.BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>

</manifest>

提前致谢。

编辑:在我的广播接收器中没有什么可看的,但这里需要的是:

package myPackage
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Utils.LogI("BootReceiver", "BootReceiver received!");
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// Do my stuff
}
}
}

最佳答案

下面这件事对我有用

AndroidManifest.xml

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

<application>

<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>

<service android:name="NotifyingDailyService" >
</service>

BootCompletedReceiver.class

public class BootCompletedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.w("boot_broadcast_poc", "starting service...");
context.startService(new Intent(context, NotifyingDailyService.class));
}

}

Service.class

 public class NotifyingDailyService extends Service {

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");

return super.onStartCommand(pIntent, flags, startId);
}
}

关于android - BOOT_COMPLETED 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20441308/

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