我是android的新手。我想在我的应用程序中显示特定日期和时间的通知(例如,2017年9月9日时间10:00 AM)。我正在使用警报管理器,但它无法正常工作。
请告诉我正确的方法。 MainActivity
公共类MainActivity扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DAY_OF_MONTH, 9);
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent inn = new Intent(MainActivity.this,AlarmActivity.class);
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0,inn, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), displayIntent);
}
AlarmActivity
公共类AlarmActivity扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.content_alarm);
final Intent notificationIntent = new Intent(this, AlarmActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(AlarmActivity.this, 0, notificationIntent, 0);
Notification notification1 = new Notification.Builder(AlarmActivity.this)
.setContentTitle("Notification")
.setContentText("Get Notigication")
.setContentIntent(contentIntent).setWhen(System.currentTimeMillis())
.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification1);
}
清单 允许
答案 0 :(得分:0)
为了触发警报,您必须使用WakefulBroadcastReceiver,您将触发将在您的活动中生成通知的服务。
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
然后你将创建一个这样的服务。
public class AlarmService extends IntentService {
public AlarmService() {
super("AlarmService");
}
@Override
public void onHandleIntent(Intent intent) {
contentId = intent.getStringExtra("id");
String title = intent.getStringExtra("title");
String subtitle = intent.getStringExtra("subtitle");
sendNotification(title, subtitle);
}
public void sendNotification(String title, String subtitle) {
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.reminder_notification_view);
contentView.setInt(R.id.layout, "setBackgroundColor",
Color.WHITE);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, title);
contentView.setTextViewText(R.id.text, subtitle);
notification.contentView = contentView;
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notification.contentIntent = PendingIntent.getActivity(this, someRandomId,
intent, 0);
notification.flags |= Notification.FLAG_AUTO_CANCEL; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(someRandomId, notification);
}
}