[英]android Boot BroadcastReceiver is not working
i have made a background service, that must run after booting the system. 我做了一个后台服务,必须在启动系统后运行。 so i crested a boot broadcast receiver, but it is not working.
所以我挂了一个引导广播接收器,但是它不起作用。 my manifest file is like this:
我的清单文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mybroadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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=".MyBroadCastReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
<activity
android:name="com.example.mybroadcastreceiver.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
and my receiver class is like this: 我的接收器类是这样的:
package com.example.mybroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0,"Broadcast Received",Toast.LENGTH_LONG).show();
//here i want to start a service,can i start service like this?
Intent service = new Intent(arg0, My_Service.class);
arg0.startService(service);
}}
Give package name for receiver in manifest file 在清单文件中给出接收者的软件包名称
<receiver
android:name="yourpackagename.MyBroadCastReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</action>
</intent-filter>
</receiver>
您忘记了在AndroidManifest.xml文件中注册My_Service
服务,只需在应用程序标签旁边按如下所示进行注册,
<service android:name="com.example.mybroadcastreceiver.My_Service" ></service>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.