문제

I have a need to set a daily alarm. I have read the many questions/solutions here and followed what I felt would work. I can see that my alarms are being set (thanks to this post). I see the Alarms set and get rescheduled for repeat 24hrs later but my receiver does not get called. so I have to assume the problem is in my implementation of the AlarmReceiver.

My Manifest file looks like this:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

     <activity
        android:name="com.TLD.testclock.TestClock"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

      <receiver android:name="com.TLD.testclock.AlarmReceiver"
         android:enabled="true"
         android:exported="true" 
         android:permission="android.permission.WAKE_LOCK" /> 

My setAlarm Code:

alarmMgr = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Intent intent = new Intent(TestClock.this, AlarmReceiver.class);
intent.putExtra("AlarmId", mAlarm.alarmId);
pendingIntent = PendingIntent.getActivity(TestClock.this,
            mAlarm.alarmId, intent, pendingIntent.FLAG_UPDATE_CURRENT);

// Set the alarm
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            alarmCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            pendingIntent);

The receiver Activity:

public class AlarmReceiver extends Activity {
private MediaPlayer mPlayer;
private WakeLock mWakeLock;

@Override
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v("AlarmReceiver", "Alarm finished?");

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "my wakelock");
    mWakeLock.acquire();
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    setContentView(R.layout.alarm_alert);
    // intent.getExtra("AlarmId", mAlarm.alarmId);
    Button stopalarm = (Button) findViewById(R.id.dismiss);

    stopalarm.setOnClickListener(new View.OnClickListener() {...

The system alarm dump shows:

RTC_WAKEUP #3: Alarm{40bc13f8 type 0 com.TLD.testclock} 

type=0 when=+6h39m58s976ms repeatInterval=86400000 count=1    

operation=PendingIntent{40b5f218: PendingIntentRecord{40bc13c0 com.TLD.testclock startActivity}}

Broadcast ref count: 0   

Alarm Stats:

com.TLD.testclock  27ms running,
2 wakeups    2 alarms: flg=0x4 cmp=com.TLD.testclock/.AlarmReceiver

I've been at this for days, but the dump tool finally proved the alarms were truly set. Does an alarm receiver have to be a broadcast receiver? I saw an alarm receiver work here so I thought I would follow the example. I get no errors, but it isn't working for me. Can anyone see what I'm doing wrong?

도움이 되었습니까?

해결책

Check the AlarmManager documentation. The AlarmManager.set() method explicitly states that it will send the Intent as a broadcast. You have your manifest setup with a receiver tag, but the class specified is an Activity rather than a BroadcastReceiver. Also, when you created your PendingIntent for registration with AlarmManager, you're creating an Intent for an Activity rather than a BroadcastReceiver. If you really need an Activity to pop up when the Alarm triggers, just have your BroadcastReceiver fire an explicit Intent for your internal Activity to show the user to alarm happened.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top