开发报警应用

时间:2010-12-19 07:38:20

标签: android alarmmanager commonsware wakelock

我想开发一个警报应用程序。 应用程序应该像这样工作:

  • 启动它
  • 活动告诉我时间
  • 我可以设置闹钟
  • 我可以关闭该应用程序
  • 当闹钟时间到来时,它会启动一项活动(即使设备被锁定)

我尝试调整此示例https://github.com/commonsguy/cwac-wakeful,但在闹钟时间到来时我无法启动活动。

我使用此代码设置警报(对于我在onCreate活动方法中插入此代码的测试):

Intent intent = new Intent(this, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);

这是OnAlarmReceiver类:

public class OnAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(ClockActivity.LOG_TAG, "OnAlarmReceiver::onReceive");
        WakefulIntentService.sendWakefulWork(context, AlarmService.class);
    }
}

这是服务类:

public class AlarmService extends WakefulIntentService {

    public AlarmService(String name) {
        super(name);
    }

    @Override
    protected void doWakefulWork(Intent intent) {
        Log.i(ClockActivity.LOG_TAG, "AlarmService::doWakefulWork");
        Intent newIntent = new Intent(getApplicationContext(), ClockActivity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        newIntent.setAction(ClockActivity.ALARM_ACTION);
        getApplicationContext().startActivity(newIntent);
    }
}

这是Manifest中设置服务和接收器的部分:

<receiver android:name=".OnAlarmReceiver"></receiver>
<service android:name=".AlarmService"></service>

永远不会调用doWakefulWork方法!

4 个答案:

答案 0 :(得分:3)

我已经提出了这个申请:

<强> AlarmActivity.java

package com.foo;
import pack.breceiver.MyBroadcastReceiver;
import android.app.Activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import android.app.Activity;
import android.os.Bundle;

public class AlarmActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void startAlert(View view) {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (i * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set in " + i + " seconds",
                Toast.LENGTH_LONG).show();
    }
}

<强> MyBroadcastReceiver.java

package pack.breceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;


public class MyBroadcastReceiver extends BroadcastReceiver  {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Don't panik but your time is up!!!!",
                Toast.LENGTH_LONG).show();

        /*// Vibrate the mobile phone
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);  */
    }
}

答案 1 :(得分:1)

检查一下:http://blog.nelsondev.net/?p=124

使用 “alarmmanager”

 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    AlarmCal.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES,
    pendingAlarmIntent);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmCal.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingAlarmIntent);

答案 2 :(得分:0)

我遇到了类似的问题,我解决了从清单中删除接收器并在我的代码中设置注册registerReceiver。

答案 3 :(得分:0)

你可能得到一个实例化异常吗?

您的服务中应该有一个public public arg构造函数:

public class AlarmService extends WakefulIntentService {

    public AlarmService() {
        super("AlarmService");
    }
    // etc
}