Android 为什么广播接收器未执行BOOT_COMPLETED(启动已完成)且未显示toast消息? 我正在尝试一个简单的例子,其中广播接收器注册为action BOOT_COMPLETED。。。但是,应用程序并没有给出toast消息。。。我找了很多。。尝试了在manifest.xml和java代码中注册接收方的两种方法。。。但我真的不知道它有什么问题。。。 请帮助我解决这个问题,因为我需要在启动时启动服务。。 提前谢谢。。
1个活动班Android 为什么广播接收器未执行BOOT_COMPLETED(启动已完成)且未显示toast消息? 我正在尝试一个简单的例子,其中广播接收器注册为action BOOT_COMPLETED。。。但是,应用程序并没有给出toast消息。。。我找了很多。。尝试了在manifest.xml和java代码中注册接收方的两种方法。。。但我真的不知道它有什么问题。。。 请帮助我解决这个问题,因为我需要在启动时启动服务。。 提前谢谢。。,android,broadcastreceiver,android-broadcast,Android,Broadcastreceiver,Android Broadcast,1个活动班 public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent arg1) { Toast.makeText(context, "Boot is completed..", Toast.LENGTH_SHORT).show(); } } 2 manifest.xml <manife
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, "Boot is completed..", Toast.LENGTH_SHORT).show();
}
}
2 manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bootupservicedemoagain"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.bootupservicedemoagain.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
</manifest>
看起来我的注册和你的启动接收器注册之间的区别在于清单中的类别
<category android:name="android.intent.category.HOME" />
显示
尝试操作权限android.intent.action.QUICKBOOT\打开您的意图过滤器。它为我解决了同样的问题。清单文件中的接收方声明应如下所示:
<receiver android:name="com.example.bootupservicedemoagain.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
此外,不需要类别。以下组合对我很有效: 在清单文件中
<!-- Get Boot Action -->
<receiver
android:name="com.example.try.BootBroadcast"
android:enabled="true"
android:exported="true"
android:label="MintBootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" >
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</action>
</intent-filter>
请告诉我这是否有帮助。有时接收器的完整路径有问题。试亲属
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
<receiver
android:name=".BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
相同的输出。。。没有显示消息您有哪台设备?我试过三星、htc、micromax tab和emulator。。但同样的输出LG Optimus G,Galaxy Tab 2 10.1您有哪款设备?我试过三星、htc、micromax tab和emulator。。但是同样的outputNexus 5,Moto G和HTC想要高清的定制rom。我刚刚意识到,如果广播接收器中没有上下文,那么发送toast消息有点困难。在服务中显示toast消息,然后重试。您可能有applicationcontext,无法显示toast消息。未工作。。。我真的不明白问题出在哪里?因为所有代码都是正确的。。谢谢你的帮助。。当我调试这个时,我使用服务来显示toast消息,而不是broadcastreceiver。每次我启动手机时,我都会收到关于所有生命周期方法oncreate等的帖子。我还注意到你的清单中没有你的服务。请参阅我的编辑。为什么我们需要android.intent.action.action\u BOOT\u COMPLETED?@IgorGanapolsky当我测试它时,它是基于设备的,一些设备制造商听取了action\u BOOT\u COMPLETED,而另一些BOOT\u COMPLETED如果路径不正确,Project将无法编译。
<receiver android:name="com.example.bootupservicedemoagain.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<!-- Get Boot Action -->
<receiver
android:name="com.example.try.BootBroadcast"
android:enabled="true"
android:exported="true"
android:label="MintBootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" >
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</action>
</intent-filter>
public class BootBroadcast extends BroadcastReceiver {
// Strings - Log
final String TAG = "BootBroadCast";
@Override
public void onReceive(Context mBootCtx, Intent mBootIntent) {
Log.i(TAG, "BootBroadCast BroadcastReceiver +++STARTED+++");
Log.d(TAG, "@Boot actionCaught :" + mBootIntent.getAction());
if ("android.intent.action.BOOT_COMPLETED".equals(mBootIntent
.getAction())) {
Log.d(TAG, "@Boot actionCaught :" + mBootIntent.getAction());
// Now you are getting your Boot reciever
}
Log.i(TAG, "BootBroadCast BroadcastReceiver ---END/FIN---");
}
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
<receiver
android:name=".BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>