尝试创建简单的警报。在这里阅读Android Dev的文章 http://developer.android.com/reference/android/provider/AlarmClock.html
我知道即使应用程序没有运行且手机处于睡眠模式,闹钟也会响起。我尝试了YT上提供的所有教程以及我可以在线搜索的所有答案,但是使用这些示例中的任何一个,当我的手机处于睡眠状态时,我无法使警报响起。我尝试在清单上添加唤醒权限,并使用了简单广播接收器的wakefulbroadcastreceier,但它没有帮助。仅当应用程序在后台运行时,警报才会响起。这是我的闹钟功能
from ***.utils import utcnow
with mock.patch.object(utcnow, '__call__', return_value=***):
...
这是Alert类
public void _setAlarm()
{
Long alerttime = new GregorianCalendar().getTimeInMillis()+60*1000;
Intent _alarmIntent = new Intent(this, Alert.class);
ArrayList<String> _displayItem = _displayItem();
_alarmIntent.putStringArrayListExtra("Text1", _displayItem);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alerttime, PendingIntent.getBroadcast(this,1,
_alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
以下是我使用自定义可扩展列表的警报活动
public class Alert extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//PendingIntent _alarmIntent = PendingIntent.getActivity(context,0, new Intent(context,Alarm.class),0);
Intent _alarmIntent = new Intent (context, Alarm.class);
ArrayList<String> data = intent.getStringArrayListExtra("Text1");
_alarmIntent.putStringArrayListExtra("Text1",data);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder _alarmBuilder = new NotificationCompat.Builder(context)
.setContentTitle("Get up")
.setContentText("Get Up")
//.setSound(uri)
.setTicker("SonsofGun")
.setSmallIcon(R.drawable.edit_icon);
TaskStackBuilder tStackBuilder = TaskStackBuilder.create(context);
tStackBuilder.addParentStack(Alarm.class);
tStackBuilder.addNextIntent(_alarmIntent);
PendingIntent _alarmPIntent = tStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
_alarmBuilder.setContentIntent(_alarmPIntent);
_alarmBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
//_alarmBuilder.setAutoCancel(true);
NotificationManager _alarmManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
_alarmManager.notify(1, _alarmBuilder.build());
}
请帮助我创建一个简单的闹钟,没有任何幻想,唤醒手机并在特定时间触发活动,即使手机处于睡眠状态且应用程序已关闭。有人说,如果应用程序已关闭,警报将永远不会触发。但无论应用程序是否正在运行,任何Android手机上的简单警报都会响起。所以它应该工作。我的最低API水平是11