我正在尝试在启动时设置警报,并让它通过AlarmManager定期调用IntentService。 AlarmReceiver永远不会被触发,不确定原因。
BootReceiver:
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());
}
}
AlarmReceiver:
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>
任何线索?
由于
答案 0 :(得分:1)
首先,删除android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
,正如您所说的那样调用您的接收者必须持有该许可,这可能是也可能不是。
然后,确保在重新启动之前运行您的一个活动,因为清单注册的BraodcastReceivers
在显式运行您的某个组件之前无法工作,通常由用户启动活动来完成。