开发报警应用程序

发布于 2024-10-08 04:13:24 字数 1909 浏览 7 评论 0原文

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

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

我已经尝试适应这个示例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 方法永远不会被调用!

I'd like develop an Alarm Application.
The application should work like this:

  • launch it
  • the activity show me the time
  • I can set the alarm
  • I can close the application
  • when the alarm time comes , it starts an activity (even if the device is locked)

I have tried to adapt this sample https://github.com/commonsguy/cwac-wakeful but I cannot launch an activity when the alarm time comes.

I use this code to setup the alarm (for test I have inserted this code on an onCreate method of activity):

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

this is the OnAlarmReceiver class:

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

this is the service 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);
    }
}

this is the portion of Manifest where are setup the service and the receiver:

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

the doWakefulWork method is never called!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮云壑 2024-10-15 04:13:24

我制作了这个应用程序:

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);  */
    }
}

I have made this application:

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);  */
    }
}
情感失落者 2024-10-15 04:13:24

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

使用“ Alarmmanager”

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

check this out : http://blog.nelsondev.net/?p=124

using"alarmmanager"

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

帅冕 2024-10-15 04:13:24

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

I had a similar problem and I resolved removing the receiver from manifest and set in my code registering him with registerReceiver.

梦毁影碎の 2024-10-15 04:13:24

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

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

public class AlarmService extends WakefulIntentService {

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

Do you get an instantiation exception maybe ?

You should have a public no arg constructor in your service :

public class AlarmService extends WakefulIntentService {

    public AlarmService() {
        super("AlarmService");
    }
    // etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文