0

打开手机时如何使用嵌套类在android中启动服务?

我的包包含嵌套类。

包裹名字

com.android

  1. 主要活动
  2. 广播接收器

我正在尝试重新启动我的广播接收器。但它不起作用(失败)。我不知道是不是因为嵌套类的任何问题。

 <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> 
4

2 回答 2

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();
    }      

}
于 2012-01-19T09:39:55.117 回答
0

你有 BroadcastReceiver 作为嵌套类。如果是,请不要这样做。

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

2)如果您在 manifest 中注册它,请为 Receiver 创建单独的类。

于 2012-01-19T09:41:43.330 回答