Alarmmanager クラスの onReceive メソッドから TextView を変更したいのですが、このクラスはアクティビティを拡張しません。アラームは、通知を作成するインテントを送信します。通知をクリックすると、この onReceive メソッドで取得した特定の文字列を含むテキストビューが開くようにしたいと考えています。
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.notify_layout);
contentView.setTextViewText(R.id.notetext,my_message);
notif.contentView = contentView;
上記のRemoteViewを通知インテントで使用してこれを実行しようとしましたが、うまくいかないようです。
findViewById を使用してこれにアクセスできないことはわかっていますが、ここでどのようなアプローチを取る必要がありますか?
これは、インテントを処理するコードからのものです
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.notify_layout);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE); nm.cancel(getIntent().getExtras().getInt("com.myapp.NotificationDisp"));
次に、この通知全体がここにあります。非推奨であることはわかっており、ビルダーを使用する必要がありますが、ビルダーではないように見えるジンジャーブレッドと互換性がある必要があります。
Intent i = new Intent("com.MyApp.NotificationDisp");
i.putExtra("com.MyApp.NotificationDisp", 1);
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
@SuppressWarnings("deprecation")
Notification notif = new Notification(R.drawable.ic_launcher,
"Myapp", System.currentTimeMillis());
//
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.notify_layout);
contentView.setTextViewText(R.id.notetext,
my_message);
notif.contentView = contentView;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
i, 0);
notif.contentIntent = contentIntent;
//
notif.setLatestEventInfo(context, "Myapp", my_message, contentIntent);
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(1, notif);
これにより、ステータスバーに正しいテキストを含む通知が表示されますが、クリックすると空白のテキストビューしか表示されません。xml notify_layout を適切に設定する必要があります。