Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

alarmmanager - Notifications in specific time every day android

I'd like to make an app that let the user decide what time everyday wants him to remind something with notification...

I'd like to know how am i supposed to trigger a notification in secific time the user wants e.x 7:00am and make him tap on the notification and enter the application in specific activity. But when the user doesn't want to get any more notifications (with a button click) how am i cancel all the notifications...?

I made something like

Intent intent = new Intent(this, main.class);
Bundle bundle = new Bundle();
bundle.putString("title", "Some Title");
intent.putExtras(bundle);   
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
String body = "Tap to see the Activity"; 
String title = "Title of notification"; 
Notification n = new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis());
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID, n);

but no luck until now....

Thanks...

<<>>

i'm doing this

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.notification_icon;
        CharSequence tickerText = "Ome TickerText"; 
        long when = System.currentTimeMillis();
        Context context = getApplicationContext();
        CharSequence contentTitle = "Some Title"; 
        CharSequence contentText = "Some Text"; 

        Intent notificationIntent = new Intent(context, LandingActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("title", "a title");
        bundle.putString("title2", "title number 2");
        bundle.putString("action", "tip");
        bundle.putString("greek", "somehting else");
        bundle.putInt("action_num", 2);
        notificationIntent.putExtras(bundle);
        alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        contentIntent = PendingIntent.getActivity(Notifications.this, 0, notificationIntent, 0);

        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        notification.defaults = Notification.DEFAULT_ALL;

        mNotificationManager.notify(UniqueID, notification);

        int hour = tp.getCurrentHour();
        int minutes = tp.getCurrentMinute();

        contentIntent = PendingIntent.getService(Notifications.this, 0, notificationIntent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, 00);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent);

but no luckk...

any help please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use alarm manager and put your notification in NotifyService class

Intent myIntent = new Intent(Current.this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);  //set repeating every 24 hours

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...