38

ユーザーがリストから時間のあるものを選択し、指定された時間に通知を作成した後、いくつかのアラームを作成しようとしています。私の問題は、私のインテントの putExtra が放送受信機で受信できない「showname」です。常に null 値を取得します。これは私の意図のほとんどで行う方法ですが、今回はおそらく pendingIntent または broadcastReceiver のために、別の方法で行う必要があると思います。ありがとうございました

保留中のインテントを介してインテントを送信する関数

public void setAlarm(String showname,String time) {

    String[] hourminute=time.split(":");
    String hour = hourminute[0];
    String minute = hourminute[1];
    Calendar rightNow = Calendar.getInstance();
    rightNow.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
    rightNow.set(Calendar.MINUTE, Integer.parseInt(minute));
    rightNow.set(Calendar.SECOND, 0);
    long t=rightNow.getTimeInMillis();
    long t1=System.currentTimeMillis();

    try {   

    Intent intent = new Intent(this, alarmreceiver.class);  
    Bundle c = new Bundle();            
    c.putString("showname", showname);//This is the value I want to pass
    intent.putExtras(c);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 12345, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(),pendingIntent);
    //Log.e("ALARM", "time of millis: "+System.currentTimeMillis());
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        Log.e("ALARM", "ERROR IN CODE:"+e.toString());
    }
}

そして、これが受信側です

public class alarmreceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();      
    Bundle b = intent.getExtras();
    String showname=b.getString("showname");//This is where I suppose to receive it but its null
    NotificationManager manger = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.icon,
            "TVGuide Υπενθύμιση", System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, tvguide.class), 0);

    notification.setLatestEventInfo(context, "Το Πρόγραμμα Ξεκίνησε",
            showname, contentIntent);

    notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;

    notification.sound = Uri.parse("file:///sdcard/dominating.mp3");
    notification.vibrate = new long[]{100, 250, 100, 500};
    manger.notify(1, notification);
}           
}
4

5 に答える 5

58

インテントで Extra の値を変更する場合は、保留中のインテントを作成するときにフラグ PendingIntent.FLAG_CANCEL_CURRENT を使用する必要があります。

簡単な例は次のとおりです。

PendingIntent pi = PendingIntent.getBroadcast(context, 0,intentWithNewExtras,PendingIntent.FLAG_CANCEL_CURRENT);

これは正しい方法であり、新しい価値が提供されることを保証します。

それが役に立てば幸い。

于 2010-10-03T20:12:36.420 に答える
24

インテントは、コンテキスト/アクションが異なる場合を除いて、システムで再利用されます。ドキュメントリンク。つまり、すでにインテントを作成している場合、そのインテントは後で使用される可能性があります。

デバッグテストとして、intent.setAction("" + Math.random())以下を追加intent.putExtras(c)して、もう一方の端でエクストラが受信されるかどうかを確認できます。

関連ドキュメント

この動作のため、PendingIntentを取得するために、2つのインテントが同じであると見なされるタイミングを知ることが重要です。よくある間違いは、「余分な」コンテンツのみが異なるIntentsを使用して複数のPendingIntentオブジェクトを作成し、毎回異なるPendingIntentを取得することを期待することです。これは起こりません。マッチングに使用されるインテントの部分は、Intent.filterEqualsで定義されたものと同じです。Intent.filterEqualsに従って同等の2つのIntentオブジェクトを使用する場合、両方に対して同じPendingIntentを取得します。

于 2010-05-21T13:49:15.853 に答える
9

同じアラーム時刻が上書きされないように、アラーム通知ごとに異なるリクエスト コードを使用してください。

于 2011-10-11T12:45:40.137 に答える
0

私は同じ問題を抱えていました。@aioobe here で提案されているように、アクションをインテントに設定して解決しました。私のインテントは魔法のように機能します。

ここで私がしたこと

  ` intent.putExtra("Name", mName);
    intent.putExtra("dateTime", newdate);
    intent.setAction("" + Math.random());`

それが誰かの助けになることを願っています.幸せなコーディング..! :)

于 2015-02-12T09:40:14.500 に答える
0

メソッドを使用する必要がありますintent.putExtra()。エクストラ セグメントにバンドルを設定しません。文字列を設定する場合は、intent.putExtra(<key>, value);これを試してみてください。使いました。

于 2013-09-30T10:48:41.113 に答える