我想從我的Android磨損項目調用AlarmManager。但由於某些原因,它失敗了。以下是我的代碼相同。我確信我正在AlarmReceiver中調用setAlarm方法(我的日誌正在打印),但之後沒有任何反應。我的目標是每30秒發出一次警報。當AlarmReceiver onReceive被調用時。 SendBroadcast到我的服務,這反過來將消息發送到服務器。我在這裏做錯了什麼。AlarmManager不工作在Android Wear
清單詳細信息:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
在應用
<receiver android:name=".alarm.AlarmReceiver" />
<receiver
android:name=".alarm.SampleBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
AlarmManager:
public class AlarmReceiver extends WakefulBroadcastReceiver {
private static final String TAG = "AlarmReceiver";
// The app's AlarmManager, which provides access to the system alarm services.
private AlarmManager alarmMgr;
// The pending intent that is triggered when the alarm fires.
private PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"AlarmReceiver onReceive*******");
Intent sensorIntent = new Intent("sensor");
LocalBroadcastManager.getInstance(context).sendBroadcast(sensorIntent);
}
public void setAlarm(Context context) {
Log.d(TAG,"Alarm Manager started");
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
System.currentTimeMillis()+1000,60000,alarmIntent);
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
}
SampleBootReceiver:
public class SampleBootReceiver extends BroadcastReceiver {
AlarmReceiver alarm = new AlarmReceiver();
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
alarm.setAlarm(context);
}
}
}
我的android Wear應用程序和移動應用程序沒有任何問題來回發送消息。我需要將我從手錶發送的信息發送到手機。爲了計時,目前我正在使用Handler.postDelayed,它工作得很好,但是我想要一直髮送消息,即使CPU進入睡眠狀態。所以我選擇了這個。 – praneel