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
<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>
我也在活动类onReceive
方法中尝试了这个但结果相同!
if(intent.getAction() != null)
{
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Toast.makeText(context, "Boot is completed..", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
尝试对您的意图过滤器执行操作权限android.intent.action.QUICKBOOT_POWERON
。它为我解决了同样的问题。清单文件中的接收者声明应如下所示:
<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>
此外,不需要类别<category android:name="android.intent.category.HOME" />
。
答案 1 :(得分:0)
我的注册和您的启动接收器注册之间的区别似乎是清单中的类别。
<category android:name="android.intent.category.HOME" />
考虑删除该行并重试。
使用我的代码进行编辑
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/**
* The onReceive method is a default method which is called when the broadcast
* receiver is registered and the intent with the action below is sent.
* This action will start the service. ACTION_BOOT_COMPLETED is defined in the
* AndroidManifest.xml.
*/
if (Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) {
Intent myIntent = new Intent(context, MyService.class);
context.startService(myIntent);
}
}
}
<强>清单强>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Handling for On Boot Receiver -->
<receiver android:name="path.to.OnBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- End Handling for On Boot Receiver -->
<service
android:name="path.to.service"
android:enabled="true"
android:exported="false" >
</service>
答案 2 :(得分:0)
以下组合对我有用: 在清单文件
<!-- 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>
和JAVA广播一样
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---");
}
请告诉我这是否有帮助。
答案 3 :(得分:0)
有时接收器的完整路径存在问题。试试亲戚
ExpressionVisitor