我想在手机启动完成后启动一项新服务。所以我为此创建广播接收器,如下所示
@Override
public void onReceive(final Context context, final Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent mServiceIntent = new Intent("com.android.reminder.BootService");
//mServiceIntent.setAction("com.android.reminder.BootService");
ComponentName service = context.startService(mServiceIntent);
if (service==null) {
// something really wrong here
Toast.makeText(context, "Sorry, Service is found null",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "Service is not null",
Toast.LENGTH_LONG).show();
}
}
}
清单文件中的声明是 -
.....
<service
android:enabled="true"
android:name="com.android.reminder.BootService">
</service>
<receiver
android:name="AlarmReceiver"
android:process=":remote" >
</receiver>
<receiver
android:enabled="true"
android:name=".BootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
</application>
我发现 Toast 是“对不起,发现服务为空”并且服务没有启动.....
请帮助我..提前谢谢..