BroadcastReceiver无法在Android 2.x上运行

时间:2014-09-21 22:18:20

标签: android android-broadcast

我正在开发一款Android应用,它使用BroadcastReceiver在手机启动时做一些工作。在运行Android 4.4.4 KitKat的Nexus 5上,它工作正常,但是我的一个朋友在运行Android 2.3.6的手机中对它进行了测试,但它没有用。我的代码如下:

public class PowerConnectionReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My app")
            .setContentText("My notification...")
            .setTicker("My awesome notification");

    NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notifManager.notify(NOTIFICATION_ID, notifBuilder.build());
}

}

在BroadcastReceiver中,我只是显示通知。然后,我在AndroidManifest.xml上注册BroadcastReceiver:

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

  ...More code...

   <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    ...More code...

   <receiver
        android:name=".activities.PowerConnectionReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    ... More code...
     </application>

如何让我的应用在Android 2.x上运行?

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试在接收器外部设置权限:

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

<application
....

将安装位置设置为internalOnly:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
android:installLocation="internalOnly"
>

最后,尝试修改接收器代码,如果以上仍然不起作用:

<receiver android:name=".activities.PowerConnectionReceiver"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>