簡體   English   中英

安卓 重新啟動我的BroadCast接收器。 不工作

[英]Android. Reboot my BroadCast receiver. Not working.

手機打開時如何使用嵌套類在android中啟動服務?

我的包中包含嵌套類。

包裹名字

com.android

  1. 主要活動
  2. BroadCast接收器

我正在嘗試重新啟動我的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> 

清單文件

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

}

您是否將BroadcastReceiver作為嵌套類。 如果是,請不要這樣做。

1)如果您將接收者作為嵌套類,則在活動中動態注冊它,而不在清單中注冊

2)如果您在清單中注冊它,請為Receiver創建單獨的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM