我做了一个后台服务,必须在启动系统后运行。所以我知道一个启动广播接收器,但它没有工作。我的清单文件是这样的:
<?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>
我的接收器类是这样的:
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);
}}
答案 0 :(得分:0)
在清单文件中为接收者提供包名称
<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>
答案 1 :(得分:0)
您忘了在AndroidManifest.xml文件中注册My_Service
服务,只需在应用程序标记旁边注册如下,
<service android:name="com.example.mybroadcastreceiver.My_Service" ></service>