0

私のアプリケーションでは、このコードを使用して通知を使用します。

 private void addDefaultNotification(){
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.icon;
    CharSequence text = "Notification Text";
    CharSequence contentTitle = "Notification Title";
    CharSequence contentText = "Sample notification text.";
    long when = System.currentTimeMillis();

    Intent intent = new Intent(this, NotificationViewer.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification = new Notification(icon,text,when);

    long[] vibrate = {0,100,200,300};
    notification.vibrate = vibrate;

    notification.ledARGB = Color.RED;
    notification.ledOffMS = 300;
    notification.ledOnMS = 300;

    notification.defaults |= Notification.DEFAULT_LIGHTS;
    //notification.flags |= Notification.FLAG_SHOW_LIGHTS;

    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);

    notificationManager.notify(com.example.NotifivcationSample.Constants.NOTIFICATION_ID, notification);
}

今、私は通知を受け取っています。その関数を呼び出すことによって。しかし、私が欲しいのは、アプリケーションがその時点でデバイス上で実行されていない場合でもユーザーに通知し、通知を希望の時間に通知する必要があることです。

出来ますか ?はいの場合は、そのために私を助けてください。ありがとう。

編集:

    public class AlarmNotificationReceiver extends BroadcastReceiver{
    //private Intent intent;
    private NotificationManager notificationManager;
    private Notification notification;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        long value1 = intent.getLongExtra("param1", 0);     
        String value2 = intent.getStringExtra("param2");

        addTwoMonthNotification();

    }
}

私はそのようにしましたが、そのレシーバークラスで通知を作成できませんでした。なんで ?そして私は何をしなければなりませんか?

4

2 に答える 2

2

はい、可能です。

インテントをAlarmManagerに登録し、通知レシーバークラスが実行時にAlarmManagerからの通知を待機するようにする必要があります。

基本的に、次のものが必要になります。

  1. 通知インテントクラス(インテントのサブクラス)

    public class MyNotificationIntent extends Intent {
        // basic implementation ...
    }
    
  2. NotificationReceiverクラス、BroadcastReceiverのサブクラス。AlarmMangerから通知を受け取り、通知を表示するためにコードを実行する必要がある場合(すでにそのようなものがあります)

    public class AlarmNotificationReceiver extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent){
    
            long value1 = intent.getLongExtra("param1", 0);
            String value2 = intent.getStringExtra("param2");
    
            // code to show the notification  
            ... 
            notificationManager.notify(...);
       }
    }
    
  3. 通知をAlarmManagerに登録するユーティリティ関数

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new MyNotificationIntent("com.alex.intent.action.ALARM_NOTIFICATION",
        Uri.parse("notification_alarm_id"), context, AlarmNotificationReceiver.class);
    intent.putExtra("param1", value1);
    intent.putExtra("param2", value2);
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, timeToRun, pendingIntent);
    
于 2011-12-23T11:41:04.913 に答える
1

http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html

AlarmManager クラスは、システム アラーム サービスへのアクセスを提供します。これらにより、将来のある時点でアプリケーションを実行するようにスケジュールできます。アラームが鳴ると、登録されていた Intent がシステムによってブロードキャストされ、ターゲット アプリケーションがまだ実行されていない場合は自動的に開始されます。

これも読むことができます:http://android.arnodenhond.com/tutorials/alarm-notification

問題なく希望の時間にユーザーに通知できるはずです

追加した

または、次のことができます。

 new Handler().postDelayed(new Runnable() { public void run() {
           //Shoot your notification here
      }
   }, 1000 * 60 * 5 );

** また **

http://developer.android.com/resources/articles/timed-ui-updates.html

private Runnable mUpdateTimeTask = new Runnable() {
   public void run() {
       final long start = mStartTime;
       long millis = //something;
       int seconds = //something;
       int minutes = //something;
       seconds     =//something;

       mHandler.postAtTime(this,
               start + (((minutes * 60) + seconds + 1) * 1000));
   }
};
于 2011-12-23T11:26:30.523 に答える