2

我有一个MyBroadcastReceiver注册为在启动时开始的课程。清单如下。我在 Android Motorola Xoom wifi only 平板电脑上通过 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>
4

2 回答 2

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>

您还在清单中指定了不正确的类名 - 它应该MyBroadcastReceiverXMyBroadcastReceiver

于 2012-10-07T08:07:32.930 回答
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 );
    }

}
于 2012-10-07T08:34:12.423 回答