如何在每天早上在android中显示推送通知

时间:2016-04-18 09:20:36

标签: android service alarmmanager

我是Android开发的新手,我希望每天在android中关闭应用程序时显示推送通知。我知道我需要为它创建服务和广播接收器,但我不想实际做到这一点。    这是我的简单推送通知代码,请帮我每天发送推送通知。

public class MainActivity extends Activity {
EditText ed1,ed2,ed3;
Notification notification;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b1=(Button)findViewById(R.id.button);
    final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    b1.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onClick(View v) {
  Notification.Builder builder = new Notification.Builder(MainActivity.this);
            Intent resultIntent = new Intent(MainActivity.this,Splash.class);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                    resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            builder.setSmallIcon(R.drawable.icon)
                    .setContentIntent(pendingIntent)
                    .setContentTitle("title");

            notification = builder.build();
            manager.notify(0, notification);
 }
    });
}}

请帮助我实现警报管理器或服务所需的功能。

1 个答案:

答案 0 :(得分:2)

您可以使用此代码在早上7:00设置闹钟

Calendar calendar = Calendar.getInstance();
// 7.00 (7 AM) 
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

PendingIntent pi = PendingIntent.getService(context, 0 , new Intent(context, Your_Class.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pi);

将此教程用于解决方案notification for a chosen time

另请参阅此Fire notification at every 24 hours and at exact time of 8 AM