广播接收器中的服务为空

时间:2013-03-05 05:42:09

标签: android null broadcastreceiver android-service

我想在完成Phone的启动时启动一项新服务。所以我为此创建了广播接收器,如下所示

   @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是“抱歉,找不到服务”并且服务未启动.....

请帮帮我.... 先谢谢..

1 个答案:

答案 0 :(得分:2)

可能你需要

 Intent mServiceIntent = new Intent(context, BootService.class);