通知欄入門

package com.pas.notification;

import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
    }

    public void click(View v) {
	// 獲取通知管理器
	// requires API level 16
	NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	// Notification.Builder builder = new Notification.Builder(this)
	// .setContentTitle("標題")
	// .setContentText("內容")
	// .setSmallIcon(R.drawable.notifications)
	// .setLargeIcon(
	// BitmapFactory.decodeResource(getResources(),
	// R.drawable.ic_launcher));
	// Notification nf = builder.build();

        //使用此API兼容性較好
	Notification nf = new Notification(R.drawable.notifications, "測試",
		System.currentTimeMillis());
	//設置點擊後自動移除
	nf.flags=Notification.FLAG_AUTO_CANCEL;
	//設置震動 等待 震動 等待……
	nf.vibrate=new long[]{1000,1000,1000,1000};
	
	// 最後一個參數設置點擊後的動做
	Intent intent=new Intent();
	intent.setAction(Intent.ACTION_CALL);
	intent.setData(Uri.parse("tel:1234"));
	PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, 0);
	nf.setLatestEventInfo(this, "標題", "內容", pendingIntent);
	nm.notify(0, nf);
    }
}
相關文章
相關標籤/搜索