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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章