0

我正在尝试在启动时设置警报,并让它通过 AlarmManager 定期调用 IntentService。AlarmReceiver 永远不会被触发,不知道为什么。

引导接收器:

package com.company.android;

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

public class BootBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Time now = new Time();
        now.setToNow();

        Log.e("#######################COMM", "Booting up " + now.toString());

        Intent alarmIntent = new Intent("com.company.android.AlarmReceiver");
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi);

        Log.e("#######################COMM", "Booted up " + now.toString());
    }
}

报警接收器:

package com.company.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by mlaino on 7/8/13.
 */
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("COMM", "Pre");


        Intent i = new Intent("com.company.android.PollingService");
        context.startService(i);

        Log.d("COMM","Pos");

    }
}

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.company.android" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
    <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="15" />
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver android:name=".BootBroadcastReceiver"
            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" />
        </intent-filter>
    </receiver>
      <receiver android:name=".AlarmReceiver">
          <intent-filter>
              <action android:name="com.company.android.AlarmReceiver" />
          </intent-filter>
      </receiver>

      <activity android:name=".CommunicationsManagerActivity">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
      <service android:name=".PollingService" >
          <intent-filter>
              <action android:name="com.company.android.PollingService" />
          </intent-filter>
      </service>
  </application>

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

有什么线索吗?

谢谢

4

1 回答 1

1

首先, remove android:permission="android.permission.RECEIVE_BOOT_COMPLETED",正如您所说,无论调用您的接收器的什么都必须持有该许可,这可能是也可能不是。

然后,请确保在重新启动之前运行您的一项活动,因为清单注册BraodcastReceivers将无法工作,直到明确运行您的组件之一,通常由用户启动一项活动来完成。

于 2013-07-09T14:53:23.563 回答