Android系统。重新启动我的BroadCast接收器。不工作。

时间:2012-01-19 09:32:20

标签: android broadcastreceiver reboot

如何在移动设备开启时使用嵌套类在Android中启动服务?

我的包中包含嵌套类。

包名称

com.android

  1. MainActivity
  2. 广播接收器
  3. 我正在尝试重新启动BroadCast接收器。但它不起作用(失败)。我不知道原因是什么,因为嵌套类有任何问题。

     <receiver android:name="ConnectionReceiver"></receiver> 
    
    <receiver android:name="com.android.MainActivity .BroadCastReceiver "> 
    <intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
    </receiver> 
    

2 个答案:

答案 0 :(得分:0)

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="onchip.automobile.caraccessory1"
    android:versionCode="1"
    android:versionName="1.0" >    

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <receiver 
            android:name=".Onchip_BroadcastReceiver">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>            
        </receiver>

        <service 
            android:name=".Onchip_BackgroundService">
            <intent-filter >
                <action android:name="onchip.automobile.caraccessory1.BACKGROUND_SERVICE" />                
            </intent-filter>            
        </service>
    </application>

</manifest>

<强> Onchip_BroadcastReceiver.java

package onchip.automobile.caraccessory1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class Onchip_BroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent serviceIntent = new Intent("onchip.automobile.caraccessory1.BACKGROUND_SERVICE");        
        context.startService(serviceIntent);
    }   
}

<强> Onchip_BackgroundService.java

package onchip.automobile.caraccessory1;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;


public class Onchip_BackgroundService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Onchip_BackgroundService Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "Onchip_BackgroundService Started", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Onchip_BackgroundService Destroyed", Toast.LENGTH_LONG).show();
    }      

}

答案 1 :(得分:0)

将BroadcastReceiver作为嵌套类。如果是的话,不要这样做。

1)如果你有接收器作为嵌套类,动态地在活动中注册它,不要在清单

中注册

2)如果您在清单中注册,请为Receiver单独创建课程。