报警接收器作为活动工作?

时间:2014-02-19 13:59:25

标签: android android-alarms

我需要设置每日闹钟。我在这里阅读了很多问题/解决方案,并遵循我认为可行的方法。我可以看到我的警报正在设置(thanks to this post)。我看到警报设置并重新安排重复24小时后,但我的接收器没有被调用。所以我必须假设问题出在AlarmReceiver的实施中。

My Manifest文件如下所示:

<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" /> 

我的setAlarm代码:

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);

接收者活动:

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() {...

系统警报转储显示:

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   

警报统计:

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

我已经在这里工作了好几天,但转储工具终于证明了警报是真的设定的。警报接收器必须是广播接收器吗?我看到alarm receiver work here,所以我想我会效仿这个例子。我没有错误,但它不适合我。谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:0)

查看AlarmManager文档。 AlarmManager.set()方法明确声明它将Intent作为广播发送。您的清单设置带有接收器标记,但指定的类是Activity而不是BroadcastReceiver。此外,当您使用PendingIntent创建AlarmManager进行注册时,您为Intent而不是Activity创建了BroadcastReceiver。如果您在Activity触发时确实需要Alarm弹出,只需让BroadcastReceiver为内部Intent显示Activity即可向用户显示发生了警报。