我正在使用下面的代码在 2 分钟后收到一条敬酒消息,同时我想用它添加一个声音警报。
Calendar cal = Calendar.getInstance();
cal.get(Calendar.DATE);
cal.add(Calendar.MINUTE,2);
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, cal.getTimeInMillis()
, pendingIntent);
Toast.makeText(this, "Time to get",
Toast.LENGTH_LONG).show();
我尝试使用以下代码:
int icon = R.drawable.ic_launcher; // icon from resources
CharSequence tickerText = "Hello"; // ticker-text
long when = cal.getTimeInMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = "My notification"; // message title
CharSequence contentText = "Hello World!"; // message text
Intent notificationIntent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 12345, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;