notification 簡單應用

直接代碼:

package com.example.notifiactionrtest;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;

import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
private Button send_notfic ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send_notfic = (Button) findViewById(R.id.send_notific);
send_notfic.setOnClickListener(this);

}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.send_notific:
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //獲得通知欄管理器
        Notification notification = new Notification(R.drawable.ic_launcher, "this is ticket", System.currentTimeMillis()); // param1 通知欄的圖像 ,param2 通知過來的提示,param3 顯示當前時間 
        Intent intent = new Intent(this,NotificActivity.class); 
        PendingIntent pi = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        // param1 context文本 ,param2 請求碼 ,param3 點擊通知欄跳轉的意圖 
        notification.setLatestEventInfo(this, "this is title", "this is text", pi);
        // 1 文本 ,2 標題, 3 部分可見得內容 ,4 點擊時的意圖
        //Uri uri = Uri.fromFile(file); // 有消息來的時候,有聲音
        //notification.sound = uri ;  
        long[] vib = {0,1000,1000,1000} ;
        notification.vibrate=vib;   // 震動 ,記得在配置文件中增加權限
        notification.ledARGB= Color.GREEN ; //led 閃光燈
        notification.ledOnMS = 1000 ; // 亮一秒
        notification.ledOffMS = 1000 ; // 暗一秒
        notification.flags = Notification.FLAG_SHOW_LIGHTS ;
        notificationManager.notify(1, notification); // 1 ID 要有多個通知欄 ,每個ID 要不一樣

        break;

    default:
        break;
    }

}

}

package com.example.notifiactionrtest;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.view.Menu;

public class NotificActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notific);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.cancel(1);// 跳轉到這個界面時,通知欄消失  1是ID 那邊設置的

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.notific, menu);
    return true;
}

}

還有兩個佈局文件 ,這裏不顯示了,一個佈局文件只是增加了一個按鈕,還有
一個只是一個TextView 顯示而已

發佈了8 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章