Android:广播接收器不会收到启动消息

时间:2012-10-07 07:49:10

标签: android android-manifest

我已注册的课程MyBroadcastReceiver在启动时启动。清单如下。我在Android摩托罗拉Xoom wifi平板电脑上通过eclipse运行这个 - 这是Android 3.2,API级别13(来自dev.android网站)。

我已经搜索过这个并尝试了一些事情:

  • android:exported="true"

  • 使用android:installLocation="internalOnly"确保它不会安装在SD卡上

  • 在设备上安装后,我在测试启动之前运行了几次应用程序(因为有些链接说Android 3.x以后会立即开始向应用程序发送启动事件,除非用户有手动启动它至少一次)

然而即使现在,当设备启动时,广播接收器似乎也没有运行(基于logcat)。

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mypack"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="internalOnly">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".MainCamApp" android:label="@string/app_name">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      </activity>

     <service android:name=".UploaderService" />

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

    </application>
</manifest>

广播接收器代码 -

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyBroadcastReceiverX extends BroadcastReceiver {
    private String TAG = "Broadcast Receiver:";
    AlarmManager alarm;

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Log.i(TAG, "Entered onReceive.");
        alarm = (AlarmManager) arg0.getSystemService(Context.ALARM_SERVICE);
        Intent intentUploadService = new Intent (arg0, UploaderService.class);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 10);

        PendingIntent pi = PendingIntent.getBroadcast(arg0, 0, intentUploadService , 0);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pi);
        Log.i(TAG, "Alarm set. Exiting onReceive.");

        Intent myIntent = new Intent(arg0, UploaderService.class);
        arg0.startService(myIntent);
    }

}

似乎现在正在工作..如果它有帮助,下面的最终清单 -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="internalOnly"
      android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

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

    <uses-sdk android:minSdkVersion="8" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".MainCamApp" android:label="@string/app_name">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      </activity>

     <service android:name=".UploaderService" />

    <receiver android:name=".MyBroadcastReceiverX"  android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED">
          <category android:name="android.intent.category.DEFAULT"/> 
          </action>
      </intent-filter>
    </receiver>

    </application>
</manifest>

2 个答案:

答案 0 :(得分:3)

尝试以下方法:

<receiver android:name=".MyBroadcastReceiverX"  android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
  <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED">
      <category android:name="android.intent.category.DEFAULT"/> 
      </action>
  </intent-filter>
</receiver>

您还在清单中指定了错误的类名 - 它应该是MyBroadcastReceiverX而不是MyBroadcastReceiver

答案 1 :(得分:1)

您需要在收到android.intent.action.BOOT_COMPLETED意图后编写代码。

试试这个。

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("TEST", intent.getAction());
    if (intent.getAction().equalsIgnoreCase(
            "android.intent.action.BOOT_COMPLETED")) {
         Log.i(TAG, "Entered onReceive.");
    alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intentUploadService = new Intent (context, UploaderService.class);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);

    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intentUploadService , 0);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pi);
    Log.i(TAG, "Alarm set. Exiting onReceive.");

    //no need to initialize intent twice
    // Intent myIntent = new Intent(context, UploaderService.class);

 context.startService(intentUploadService );
    }

}