Android開発 虎の巻

通知

■トースト

Toast toast = Toast.makeText(this, "テスト", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 0)
toast.show();

一行での表記

Toast.makeText(this, "テスト", Toast.LENGTH_LONG).show();

■通知

通知バーにメッセージを表示する。

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentTitle("通知テスト");
builder.setContentText("テストです");
builder.setAutoCancel(true);
builder.setTicker("テスト");
builder.setSmallIcon(R.drawable.ic_launcher);

NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(0, builder.build());

通知をタップで画面表示

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentTitle("通知2");
builder.setContentText("画面を開く");
builder.setTicker("テスト2");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(pi);

NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(0, builder.build());
Android開発 虎の巻