【问题标题】:Set Notification to a specific time将通知设置为特定时间
【发布时间】:2015-10-10 22:34:25
【问题描述】:

我想将通知设置为在特定时间显示,例如上午 10 点。我已经在网上搜索并做到了这一点,但我不知道出了什么问题,因为没有任何通知!这是我的通知类:

public class notification extends AppCompatActivity {
    NotificationManager manager;
    Notification myNotication;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent intent = new Intent(notification.this,about.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(notification.this, 1, intent, 0);

        Notification.Builder builder = new Notification.Builder(notification.this);

        builder.setAutoCancel(false);
        builder.setTicker("this is ticker text");
        builder.setContentTitle("WhatsApp Notification");
        builder.setContentText("You have a new message");
        builder.setSmallIcon(R.drawable.up);
        builder.setContentIntent(pendingIntent);
        builder.setOngoing(true);
        builder.setNumber(100);
        myNotication = builder.getNotification();
        manager.notify(0, myNotication);
    }

还有我的主类(活动)(onCreate 方法):

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(main.this,notification.class);
        intent.putExtra("uur", "1e");
        pendingIntent = PendingIntent.getBroadcast(main.this, 0, intent, 0);
        timeOff9 = Calendar.getInstance();
        timeOff9.set(Calendar.HOUR_OF_DAY, 01);
        timeOff9.set(Calendar.MINUTE, 46);
        timeOff9.set(Calendar.SECOND, 0);
        alarmMgr0.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent);

【问题讨论】:

    标签: java android notifications alarm


    【解决方案1】:

    我认为您应该根据需要修改以下代码

    private void handleNotification() {
        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, System.currentTimeMillis(), 5000, pendingIntent);
    }
    

    这是自定义的 BroadcastReceiver

    public class AlarmReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Calendar now = GregorianCalendar.getInstance();
            int dayOfWeek = now.get(Calendar.DATE);
            if(dayOfWeek != 1 && dayOfWeek != 7) {
                NotificationCompat.Builder mBuilder = 
                        new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(context.getResources().getString(R.string.message_box_title))
                        .setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date));
                Intent resultIntent = new Intent(context, MainActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(MainActivity.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(1, mBuilder.build());
            }
    
        }
    }
    

    并添加清单应用标签:

    <receiver
            android:name="be.menis.timesheet.service.AlarmReceiver"
            android:process=":remote" />
    

    根据需要修改此代码,我认为这很有用

    【讨论】:

    • 感谢您的回答,我的API lvl 是14,请问如何设置具体时间?
    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多