2012-03-16 13 views
0

ここに私のコードです。アンドロイドでの通知の問題を伴うアラームの生成

Calendar cal = Calendar.getInstance(); 
    int id = (int) cal.getTimeInMillis(); 
    Intent myIntent = new Intent(this,MyScheduledReceiver.class); 
    myIntent.putExtra("taskTitle", taskTitle.getText().toString()); 
    myIntent.putExtra("taskDetails", taskDetails.getText().toString()); 

    cal.setTimeInMillis(System.currentTimeMillis()); 
    cal.set(year, mon, day,tp.getCurrentHour(),tp.getCurrentMinute()); 
    PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), id, 
      myIntent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA); 
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
      am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

..私はプログラム的にアラームを生成することはできませんよどこから 私は、ブロードキャストレシーバにアン

<receiver android:name=".MyScheduledReceiver"></receiver> 

マニフェストファイルで

を宣言しました。

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
NotificationManager manger = (NotificationManager)  
context.getSystemService(Context.NOTIFICATION_SERVICE); 
Notification notification = new Notification(R.drawable.ic_launcher, "Combi Note", 
System.currentTimeMillis()); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 
NOTIFICATION_ID, 
new Intent(context, MyScheduledReceiver.class), 0); 
     Bundle extras=intent.getExtras(); 
     String title=extras.getString("taskTitle"); 
    String note=extras.getString("taskDetails"); 
     notification.setLatestEventInfo(context, note, title, contentIntent); 
     notification.flags = Notification.FLAG_INSISTENT; 
     notification.defaults |= Notification.DEFAULT_SOUND; 
    manger.notify(NOTIFICATION_ID++, notification); 
+0

私はちょっと混乱しています - アラームを生成することができないと言いましたが、これはReceiverのonReceive(...)が呼び出されないことを意味します。これは本当ですか? – Nick

+0

私はそれが正しい方法であるよりも通知をgenerizeしたい場合は残してください...それはリマインダの一種です..私はあなたの意見を持っていると思います。 2012年3月17日午後7時に仕事の例を通知 –

+0

私はあなたのアラームコードに明白なエラーがないので、あなたが設定しているアラーム時刻のような何かがあなたが期待しているものではない、簡単にするために、私はあなたのレシーバーのonReceive(...)の内部にトーストメッセージを表示するだけです。また、あなたのcal.set(...)呼び出しをコメントアウトし、cal.setTimeInMillisをcal.setTimeInMillis(System.currentTimeMillis()+ 5000)に変更します。そのため、アプリを実行するときに、Toastメッセージが表示されない場合は、起動していないことがわかります。 – Nick

答えて

0

私は何か類似しており、動作します。

まずハイテク保留を宣言:

Intent intent = new Intent(Global.a, EventAlarmReceiver.class); 
    intent.putExtra("title", ""+cd.title); 
    intent.putExtra("desc", ""+cd.description); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      Global.a.getApplicationContext(), (int) (cd.alarmTime), intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    AlarmManager alarmManager = (AlarmManager) Global.a.getSystemService(Activity.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC, cd.alarmTime, pendingIntent); 

とEventAlarmReceiverクラスで私が持っている:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); 

    String text = String.valueOf(intent1.getCharSequenceExtra("title")); 

    Notification notification = new Notification(R.id.icon, 
      text, System.getTimeInMillis()); 

    notification.vibrate = new long[]{100,250,300,330,390,420,500}; 

    // Hide the notification after its selected  
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    Intent intent = new Intent(context, ShowEventActivity.class); 
    intent.putExtra("title", String.valueOf(intent1.getCharSequenceExtra("title"))); 
    intent.putExtra("desc", String.valueOf(intent1.getCharSequenceExtra("desc"))); 


    String text1 = String.valueOf(intent1.getCharSequenceExtra("desc")); 

    PendingIntent activity = PendingIntent.getActivity(context, (int) System.getTimeInMillis() , intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    notification.setLatestEventInfo(context, text, text1, activity); 

    notificationManager.notify((int)System.getTimeInMillis(), notification); 

マニフェストにあなたはレシーバーがあるパッケージ宣言宣言していることを確認します。たとえばにします私の場合、EventAlarmReceiverクラスはパッケージcom.app.name.notificationsですので、マニフェストには次のものがあります。

<receiver android:name=".notifications.EventAlarmReceiver"></receiver> 
関連する問題