android 本地提醒功能,android – 即使应用程序关闭,也会在特定时间每天触发本地通知...

即使我的应用程序没有运行,我也必须每天在特定时间(比如早上7点)向我的应用程序显示通知.(已关闭)

由于通知的内容将是用户特定的,我无法使用(GCM)推送

我已经完成了很多教程和答案但是我无法在应用程序关闭(不运行)时显示通知.

编辑:使用以下代码我可以在我的应用程序运行时创建通知,如果我关闭我的应用程序通知正在显示

这是我的主要活动

public class MainActivity extends AppCompatActivity {

Button setAlarm;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

setAlarm = (Button) findViewById(R.id.set_alarm);

setAlarm.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

NotificationEventReceiver.setupAlarm(getApplicationContext());

}

});

}

@Override

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

setIntent(intent);

}

}

在这里,当我单击设置警报按钮时,我的NotificationEventReciver将调用

public class NotificationEventReceiver extends WakefulBroadcastReceiver {

private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";

private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";

public static void setupAlarm(Context context) {

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(context, NotificationEventReceiver.class);

PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, getTriggerAt(new Date()), 2 * 60 * 1000, alarmIntent);

}

private static long getTriggerAt(Date now) {

Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

calendar.set(Calendar.HOUR_OF_DAY, 17);

calendar.set(Calendar.MINUTE, 10);

System.err.println(now.getTime()+"----->"+ calendar.getTimeInMillis());

return calendar.getTimeInMillis();

}

public static void cancelAlarm(Context context) {

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

PendingIntent alarmIntent = getStartPendingIntent(context);

alarmManager.cancel(alarmIntent);

}

private static PendingIntent getStartPendingIntent(Context context) {

Intent intent = new Intent(context, NotificationEventReceiver.class);

intent.setAction(ACTION_START_NOTIFICATION_SERVICE);

return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

public static PendingIntent getDeleteIntent(Context context) {

Intent intent = new Intent(context, NotificationEventReceiver.class);

intent.setAction(ACTION_DELETE_NOTIFICATION);

return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

@Override

public void onReceive(Context context, Intent intent) {

Intent serviceIntent = NotificationIntentService.createIntentStartNotificationService(context);

if (serviceIntent != null) {

System.err.println("onReceive inside not null");

// Start the service, keeping the device awake while it is launching.

startWakefulService(context, serviceIntent);

}

}

}

我的服务类将在闹钟响起时运行.

public class NotificationIntentService extends IntentService {

private static final int NOTIFICATION_ID = 1;

private static final String ACTION_START = "ACTION_START";

private static final String ACTION_DELETE = "ACTION_DELETE";

public NotificationIntentService() {

super(NotificationIntentService.class.getSimpleName());

}

public static Intent createIntentStartNotificationService(Context context) {

Intent intent = new Intent(context, NotificationIntentService.class);

intent.setAction(ACTION_START);

return intent;

}

@Override

protected void onHandleIntent(Intent intent) {

System.err.println("onHandleIntent, started handling a notification event");

try {

String action = intent.getAction();

if (ACTION_START.equals(action)) {

System.err.println("enters the loop ACTION_START");

processStartNotification();

}

} finally {

WakefulBroadcastReceiver.completeWakefulIntent(intent);

}

}

private void processStartNotification() {

// Do something. For example, fetch fresh data from backend to create a rich notification?

System.err.println("inside the notify method");

Intent mainIntent = new Intent(this, MainActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setContentTitle("Scheduled Notification")

.setAutoCancel(true)

.setColor(getResources().getColor(R.color.colorAccent))

.setContentText("This notification has been triggered by Notification Service")

.setSmallIcon(R.drawable.ic_launcher);

builder.setContentIntent(pendingIntent);

final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

manager.notify(NOTIFICATION_ID, builder.build());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值