0

爲了趕上android.intent.action.BOOT_COMPLETED事件,只需在AndroidManifest.xml中註冊BroadcastReceiver就足夠了嗎?像這樣:爲了捕獲BOOT_COMPLETED,僅僅在`AndroidManifest.xml`中註冊BroadcastReceiver就足夠了?

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.myDomain.myApp" 
      android:installLocation="internalOnly"> 
    <!-- ... stuff ... --> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <!-- ... stuff ... --> 
    <application ...> 
     <!-- ... stuff ... --> 
     <receiver android:name=".bgServices.MyBootBroadcastReceiver"> 
     </receiver> 
     <!-- ... stuff ... --> 
    </application> 
</manifest> 

BroadcastReceiver

package com.myDomain.myApp.bgServices; 

// imports ... 

public class MyBootBroadcastReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("my-log", "MyBootBroadcastReceiver.onReceive()"); 
     Toast.makeText(context, "YEAY!", Toast.LENGTH_LONG).show();  
    } 
} 

我這麼問是因爲我要送:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.myDomain.myApp/.bgServices.MyBootBroadcastReceiver

但沒有任何反應。

(我運行的應用程序,不只是推到設備)

+0

@jankigadhiya,看到我的文章的最後一行:「(我運行的應用程序,不只是推到設備)」 – Tar

回答

1

添加以下IntentFilter s到您的BroadcastReceiver

<receiver android:name=".bgServices.MyBootBroadcastReceiver" > 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
    </intent-filter> 
</receiver> 

雖然android.intent.action.BOOT_COMPLETED應該夠了,似乎有些設備需要android.intent.action.QUICKBOOT_POWERON也。

欲瞭解更多的信息,你應該看看developer's guideIntent s和IntentFilter s。

1

不,這不是。您未指定將收到ACTION android.intent.action.BOOT_COMPLETED的組件。所以你需要添加intent-filter到你的接收器

你應該像下面這樣做。

<receiver android:name=".bgServices.MyBootBroadcastReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    </intent-filter> 
</receiver> 
相關問題