1

私はアンドロイドが初めてです。通知をローカルに設定したい。そして、特定の日時に発射したい。しかし、それを未来に設定すると、すぐに起動しますか?

 Calendar calendar = Calendar.getInstance();       

         //---sets the time for the alarm to trigger---
         calendar.set(Calendar.YEAR, 2013);
         calendar.set(Calendar.MONTH, 1);
         calendar.set(Calendar.DAY_OF_MONTH, 11);                 
         calendar.set(Calendar.HOUR_OF_DAY, 15);
         calendar.set(Calendar.MINUTE, 54);
         //calendar.set(calendar.AM_PM,1);
         calendar.set(Calendar.SECOND, 0);


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

        int icon = R.drawable.alp_btn_code_lock_touched_holo;        
        CharSequence tickerText = "Hello"; // ticker-text
        long when = System.currentTimeMillis();         
        Context context = getApplicationContext();     
        CharSequence contentTitle = "Hello";  
        CharSequence contentText = "Hello";      
        Intent notificationIntent = new Intent(this, Login.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, calendar.getTimeInMillis());
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);


        mNotificationManager.notify(1, notification);

手伝ってくれてありがとう

4

2 に答える 2

0

BroadcasrreceiverでAlarmmanagerを使用する と、コードが次のように変更されるので便利です。

 Calendar calendar = Calendar.getInstance();       

     //---sets the time for the alarm to trigger---
     calendar.set(Calendar.YEAR, 2013);
     calendar.set(Calendar.MONTH, 1);
     calendar.set(Calendar.DAY_OF_MONTH, 11);                 
     calendar.set(Calendar.HOUR_OF_DAY, 15);
     calendar.set(Calendar.MINUTE, 54);
     //calendar.set(calendar.AM_PM,1);
     calendar.set(Calendar.SECOND, 0);

  Intent intentval = new Intent(YourActivity.this, TimeAlarm.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentval, 0);
    alarm.set(AlarmManager.ELAPSED_REALTIME,  calendar.getTimeInMillis() , pendingIntent);

このようにTimeAlarm.class使用中

public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {     

    int notificationId = Integer.parseInt(intent.getData().getSchemeSpecificPart());     

    Intent alarmIntent = new Intent(context, TimeAlarm.class);
    alarmIntent.setData(Uri.parse("timer:" + notificationId));
    //alarmIntent.setAction(String.valueOf(alarm.ID));
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent displayIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
    alarmManager.cancel(displayIntent);     

    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);       

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    Notification notif = new Notification(icon, tickerText, System.currentTimeMillis());
    notif.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    nm.notify(1, notif);        
于 2013-02-11T11:07:07.730 に答える
0

これには、通知を直接使用する代わりに、AlarmManager を使用します。このためにも、BroadCastReceiver チェックアウト リンクを実装する必要があります。

http://smartandroidians.blogspot.in/2010/04/alarmmanager-and-notification-in.html

http://maephv.blogspot.in/2011/08/android-alarmmanager-and-notification.html

于 2013-02-11T10:39:47.887 に答える