无法控制通知管理器 android
Can't control notification manager android
我有一个 application
应该每天在准确的时间发送 notifications
。及时触发没关系,但每次我关闭应用程序 notification
时都会出现,但它不会。
通知class:
@Override
public int onStartCommand(Intent intent,int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
Context context = getApplicationContext();
Intent resultIntent = new Intent(getApplicationContext(), Love.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, resultIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
/* Invoking the default notification service */
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("New Message");
mBuilder.setContentText("You've received new message.");
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentIntent(contentIntent);
android.app.Notification notification = mBuilder.build();
notification.defaults = notification.DEFAULT_SOUND |
notification.DEFAULT_VIBRATE;
notification.flags |= notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
return Service.START_STICKY;
}
主要活动:
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 10);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
long day = TimeUnit.HOURS.toMillis(24);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),day, pendingIntent);
和广播接收器:
public class AlarmReceiver 扩展 BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, Notification.class);
context.startService(service1);
}
}
接收方:
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, Notification.class);
context.startService(service1);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Boolean notif = sharedPreferences.getBoolean("notif", true);
if (!notif) context.stopService(service1);
}
Notification
必须只在 10:00 发送,但它有非常奇怪的行为和方法 stopService
不是每次都有效(即使是 false)。我想那是带有标志的东西,但我仍然不知道我到底做错了什么。
提前致谢。而且,请不要低估我的问题,即使它非常简单或只是愚蠢。我是新手,只是想知道。
你只需要销毁服务,完成后:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
stopSelf()
return Service.START_STICKY;
我有一个 application
应该每天在准确的时间发送 notifications
。及时触发没关系,但每次我关闭应用程序 notification
时都会出现,但它不会。
通知class:
@Override
public int onStartCommand(Intent intent,int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
Context context = getApplicationContext();
Intent resultIntent = new Intent(getApplicationContext(), Love.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, resultIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
/* Invoking the default notification service */
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("New Message");
mBuilder.setContentText("You've received new message.");
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentIntent(contentIntent);
android.app.Notification notification = mBuilder.build();
notification.defaults = notification.DEFAULT_SOUND |
notification.DEFAULT_VIBRATE;
notification.flags |= notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
return Service.START_STICKY;
}
主要活动:
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 10);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
long day = TimeUnit.HOURS.toMillis(24);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),day, pendingIntent);
和广播接收器:
public class AlarmReceiver 扩展 BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, Notification.class);
context.startService(service1);
}
}
接收方:
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, Notification.class);
context.startService(service1);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Boolean notif = sharedPreferences.getBoolean("notif", true);
if (!notif) context.stopService(service1);
}
Notification
必须只在 10:00 发送,但它有非常奇怪的行为和方法 stopService
不是每次都有效(即使是 false)。我想那是带有标志的东西,但我仍然不知道我到底做错了什么。
提前致谢。而且,请不要低估我的问题,即使它非常简单或只是愚蠢。我是新手,只是想知道。
你只需要销毁服务,完成后:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
stopSelf()
return Service.START_STICKY;