开发警报应用

编程入门 行业动态 更新时间:2024-10-26 00:19:46
本文介绍了开发警报应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想开发一个报警的应用。 该应用程序应该是这样的:

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

  • 启动它
  • 活动让我的时间
  • 我可以设置报警
  • 我可以关闭应用程序
  • 当报警时间已到,开始一个活动(即使设备处于锁定状态)

我试图去适应这个样本 github/commonsguy/cwac-wakeful但我不能启动一个活动时,报警时间已到。

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

我用这个code设置的报警器(用于测试我都插在活动的的onCreate 方法,此code):

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

这是OnAlarmReceiver类:

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

这是服务类:

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>

在doWakefulWork方法不会被调用!

the doWakefulWork method is never called!

推荐答案

我做了这个应用程序:

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

更多推荐

开发警报应用

本文发布于:2023-11-27 01:25:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635998.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!