2012年5月18日 星期五

[Android] [JAVA] Notification & PendingIntent
















1). 在第一個Activity設置Button,觸發註冊Notification

2). 宣告intent,並指定原本class及要轉換的Activity

3). 將此intent用PendingIntent包起來

4). 設定flag

5). 設定出現在狀態列的文字

6). 送出Notification

//=============================================================


[strings.xml]

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, NotificationTest1Activity!</string>
  <string name="app_name">NotificationTest1</string>
  <string name="textView1">Press the button to start notification bar</string>
  <string name="textView2">HELLO! This is Activity2</string>
  <string name="notification">Notification</string>
</resources>



//-------------------------------------------------------------------------------------------------------------

[main.xml]
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/textView1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/notification" />

</LinearLayout>

//-------------------------------------------------------------------------------------------------------------

[mylayout.xml]
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/textView2"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

//-------------------------------------------------------------------------------------------------------------

[NotificationTest1.java]
package com.notificationtest;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NotificationTest1Activity extends Activity {
  private Button button;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(startClickListener);
  }
  private Button.OnClickListener startClickListener=new Button.OnClickListener(){
    @Override
    public void onClick(View v) {
      //取得Notification服務
      NotificationManager notificationManager=(NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
      //設定當按下這個通知之後要執行的activity
      Intent notifyIntent = new Intent
          (NotificationTest1Activity.this,TestApp.class);
      notifyIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
      PendingIntent appIntent=PendingIntent.getActivity
              (NotificationTest1Activity.this,0,notifyIntent,0);
      Notification notification = new Notification();
      //設定出現在狀態列的圖示
      notification.icon=R.drawable.ic_launcher;
      //顯示在狀態列的文字
      notification.tickerText="notification on status bar.";
      //Notification被點擊後,便消失
      notification.flags|=Notification.FLAG_AUTO_CANCEL;
      //會有通知預設的鈴聲、振動、light
      notification.defaults|=Notification.DEFAULT_ALL;
      //設定通知的標題、內容
      notification.setLatestEventInfo(NotificationTest1Activity.this,
                        "Title","content",appIntent);
      //送出Notification
      notificationManager.notify(0,notification);
    }
  };
}

//-------------------------------------------------------------------------------------------------------------

[TestApp.java]
package com.notificationtest;
import android.app.Activity;
import android.os.Bundle;

public class TestApp extends Activity{
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.mylayout);
  }
}

//-------------------------------------------------------------------------------------------------------------

[AndroidMenifest.xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.notificationtest"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk android:minSdkVersion="15" />
  // 宣告振動的permission
  <uses-permission android:name="android.permission.VIBRATE" />
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
      android:name=".NotificationTest1Activity"
      android:label="@string/app_name" >
      <intent-filter>
      <action android:name="android.intent.action.MAIN" />

      <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    // 註冊另外一個Activity
    <activity android:name="TestApp"></activity>
  </application>
</manifest>

沒有留言:

張貼留言