Broadcast Receiver not working immediately after package installation

六眼飞鱼酱① 提交于 2019-12-25 01:37:19

问题


I am currently working on an app that receives the BOOT_COMPLETED action using a Broadcast Receiver. The receiver is statically registered in the AndroidManifest.xml.

It works on the next boot after installation for android 2.2 but not 3.1. With 3.1 I have to start the app once before the broadcast receiver gets the BOOT_COMPLETED action when booting.

I suspect that something changed in 3.1 that is causing my receiver to be inactive until the app is started once.

Has anyone ever seen this before or know how to stop this behavior? Any Help would be greatly appreciated. Thanks, Tom.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.receiver"
    android:versionCode="1"
    android:versionName="1.0">
  <uses-sdk android:minSdkVersion="8" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name="TestReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
  </application>
</manifest>

TestReceiver.java:

package com.test.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class TestReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.i("TestReceiver", "onReceive() was called");    
  }
}

MainActivity.java:

package com.test.receiver;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

回答1:


I found out this is a policy change in 3.1. An app remains inactive after installation until the user launches it for the first time.



来源:https://stackoverflow.com/questions/6528892/broadcast-receiver-not-working-immediately-after-package-installation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!