在android.permission.RECEIVE_BOOT_COMPLETED中实现服务
问题描述:
我在网络搜索中采用下面的示例。我对该示例进行了一些修改,但在重新启动手机后显示错误。我也我的示例中使用粘贴的源代码,请帮我解决问题在android.permission.RECEIVE_BOOT_COMPLETED中实现服务
在此先感谢
xml文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newtest"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application>
<receiver android:name=".BootCompletedIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".BackgroundService"/>
</application>
</manifest>
广播接收器类
package com.example.newtets;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, BackgroundService.class);
context.startService(pushIntent);
}
}
}
服务等级
package com.example.newtets;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class BackgroundService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
super.onCreate();
}
}
答
我不认为你需要比较的意图..
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
.....
}
因为,它是在BootCompletedIntentReceiver应该BOOT_COMPLETED意图执行的xml文件抓获。
什么错误你越来越PLZ也添加日志的问题 – 2013-02-15 12:33:55
我无法获得日志(我不知道如何获取服务应用程序在启动手机后执行日志),重新启动手机后,应用程序崩溃出现意外错误 – Riskhan 2013-02-15 12:35:05
为什么如果您要启动服务而不是活动,请设置FLAG_ACTIVITY_NEW_TASK? – Rafael 2013-02-15 12:43:26