ノーティフィケーションを表示する |
Androidには画面上部にステータスバーという領域があります。 例えば、画面に表示されていないアクティビティやバックグラウンドで動作するプロセスからユーザに何か伝えたい場合に用いられます。 ノーティフィケーションを表示するには、ノーティフィケーションをクリックした際に実行する「PendingIntent」インスタンスを生成します。※サンプルではURLを指定して、クリック時に画面遷移します。 NotificationActivity.java package goodroid.sample.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; public class NotificationActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Notificationクリック時に開くURLを設定 String url = "http://goodroid.fc2-rentalserver.com/"; Uri uri = Uri.parse(url); // Notificationクリック時のPendingIntentを生成 Intent intent = new Intent(Intent.ACTION_VIEW, uri); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); // NotificationManagerインスタンスを取得 NotificationManager nManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); // Notificationインスタンスを生成 Notification notification = new Notification(); //通知ウィンドウから選択された後で自動的に通知をキャンセル notification.flags = Notification.FLAG_AUTO_CANCEL; // アイコンをセット notification.icon = R.drawable.icon; // 通知時のテキストをセット notification.tickerText = "通知テキスト表示"; // Notificationバーを開いたときに表示される内容をセット notification.setLatestEventInfo( getApplicationContext(), "タイトルを表示", "メッセージを表示", pendingIntent ); // Notificationを通知 nManager.notify(1, notification); } } 実行結果 ![]() ![]() ![]() |
4522 views | コメント:0 | 2012-06-10 |
コメント
|
|
まだこの記事にコメントはありません |