Android/通知
提供: 初心者エンジニアの簡易メモ
2015年5月20日 (水) 03:16時点における127.0.0.1 (トーク)による版 (ページの作成:「ステータスバー上部に新着メッセージを表示する *AndroidHelloworld1.java package info.nonip.AndroidHelloworld1; import android.os.Bundle; import an...」)
ステータスバー上部に新着メッセージを表示する
- AndroidHelloworld1.java
package info.nonip.AndroidHelloworld1;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.app.PendingIntent;
import android.net.Uri;
public class AndroidHelloworld1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// マニフェストに追加 <uses-permission android:name="android.permission.VIBRATE" />
// 通知センター管理取得
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 通知情報生成
Notification notification = new Notification(
//android.R.drawable.btn_default,
R.drawable.ic_launcher,
"アプリ新着情報が届きました",
System.currentTimeMillis());
// アプリ選択画面
// Intent intent = new Intent(Intent.ACTION_VIEW);
// 電話画面
// Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0300000000"));
// 指定activity起動
Intent intent = new Intent(getApplicationContext(), AndroidHelloworld1Activity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
// 通知イベント情報設定
notification.setLatestEventInfo(
getApplicationContext(),
"アプリ1",
"通知本文です",
contentIntent
);
// 管理に通知情報を設定
notificationManager.notify(R.string.app_name, notification);
}
}
- AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE" />
