Android入門ノート-インタフェース開発-Notification,NotificationManager


今日はandroidのお知らせに触れてみましょう.
  • Notification
  • NotificationManager

  • 中には2つのactivityが含まれているので、もう2つのlayoutファイルがあります.1つはメインプログラムで、1つは通知バーのアイコンをクリックしてポップアップしたactivityで、直接コードをつけます.
    1. res/layout/activity_main.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    	
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="    ,    "/>
        <Button 
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1"/>
        
    </LinearLayout>

    2. res/layout/activity_notification.xml:通知ジャンプページをクリック
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="      "
            android:textSize="30sp" />
    
    </LinearLayout>

    3. src/MainActivity.java
    package com.example.demoui5_notification;
    
    import android.os.Bundle;
    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.view.Menu;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    	private Context ctx = this;
    	private Button mBtn1;
    	private NotificationManager notificationManager;
    	private Notification notification;
    	private PendingIntent pendingIntent;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    		Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
    		pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
    		notification = new Notification();
    		
    		mBtn1 = (Button) findViewById(R.id.btn1);
    		mBtn1.setOnClickListener(new View.OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				notification.icon = R.drawable.ic_launcher;
    				notification.tickerText = "Button1     ";
    				notification.defaults = Notification.DEFAULT_SOUND;
    				notification.setLatestEventInfo(ctx, "Button1", "Button1  ", pendingIntent);
    				notificationManager.notify(0, notification);
    			}
    		});
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    

    4. src/NotificationActivity.java
    package com.example.demoui5_notification;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class NotificationActivity extends Activity {
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_notification);
    	}
    }
    

    この例は簡単です.Button 1ボタンをクリックすると、画面上に通知があり、通知をクリックして寧外のページにジャンプします.
    重要なコンポーネントは、NotificationManagerとNotification、PendingIntentはIntentへの拡張です.使い方を見てみましょう.
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
    pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
    notification = new Notification();
    		
    mBtn1 = (Button) findViewById(R.id.btn1);
    mBtn1.setOnClickListener(new View.OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		notification.icon = R.drawable.ic_launcher;
    		notification.tickerText = "Button1     ";
    		notification.defaults = Notification.DEFAULT_SOUND;
    		notification.setLatestEventInfo(ctx, "Button1", "Button1  ", pendingIntent);
    		notificationManager.notify(0, notification);
    	}
    });
    まずgetSystemService(NOTIFICATION_SERVICE)で通知管理者を得るが、クリック通知送信のintentと直接activityでintentを送信するのとでは異なるため、ここではPendingIntentを用いており、構成は上記の通りである.次に通知オブジェクト(Notification)を構築し、icon、タイトルバーに表示される文字、音声、ドロップダウンタイトルバーに表示される通知スタイルを設定したり、pendingIntentをバインドしたりすることができます.
    最後にmanagerがnotifyのNotifficationに来て、よし、完成.