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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.