我正在创建一个类似于闹钟的应用程序,并且想知道如何做一些事情。
我可以在某个特定时间获得声音通知,我想知道你如何编辑它(删除旧的)以及如何让它每5分钟发出一次直到我决定的某个时间
我在2010年1月1日7:00创建了通知 我说在设置中应该每隔5分钟发声一次,直到2001年1月2日
我该如何实现?
班级广播接收器
public void onReceive(Context context, Intent intent) {
Vibrator vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
Intent i = new Intent(context, NotificationReceiverActivity.class);
PendingIntent oi = PendingIntent.getActivity(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setContentIntent(oi).setSmallIcon(R.drawable.avvio)
.setContentTitle(context.getString(R.string.notific_title))
.setContentText(context.getString(R.string.notific_subject));
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/"
+ R.raw.audio);
mBuilder.setSound(sound);
mgr.notify(0, mBuilder.build());
}`
类notificationreceiver只是一个布局..
class allarm
public void createAlarm(String data,String ogni){
StringTokenizer s = new StringTokenizer(data," ");
String date = s.nextToken();
String time = s.nextToken();
StringTokenizer a = new StringTokenizer(date,"/");
StringTokenizer v = new StringTokenizer(time,":");
String giorno = a.nextToken();
String mese = a.nextToken();
String anno = a.nextToken();
String ora = v.nextToken();
String minuti = v.nextToken();
Calendar myAlarmDate = Calendar.getInstance();
myAlarmDate.setTimeInMillis(System.currentTimeMillis());
myAlarmDate.set(Integer.valueOf(anno), Integer.valueOf(mese)-1, Integer.valueOf(giorno), Integer.valueOf(ora), Integer.valueOf(minuti), 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent _myIntent = new Intent(context, MyBroadcastReceiver.class);
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myAlarmDate.getTimeInMillis(),_myPendingIntent);
答案 0 :(得分:0)
您可以在应用的BroadcastReceiver
中进行重置:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive( Context context, Intent src ) {
Date now = new Date();
Date until = getUntilDateFromPreferencesForExample();
if( now.after( until ) ){
PendingIntent pi = PendingIntent.getBroadcast( app, 0, src, PendingIntent.FLAG_UPDATE_CURRENT );
((AlarmManager)context.getSystemService( Context.ALARM_SERVICE )).cancel( pi );
}
}
}