13

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

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

我已尝试调整此示例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

4 回答 4

3

我做了这个应用程序:

报警活动.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);  */
    }
}
于 2012-06-04T06:21:41.933 回答
1

看看这个:http: //blog.nelsondev.net/ ?p=124

使用“报警管理器”

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

于 2012-05-01T12:19:54.940 回答
0

您可能会遇到实例化异常吗?

您的服务中应该有一个公共的无参数构造函数:

public class AlarmService extends WakefulIntentService {

    public AlarmService() {
        super("AlarmService");
    }
    // etc
}
于 2013-04-03T20:19:22.893 回答
0

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

于 2012-03-19T23:35:34.430 回答