您的位置:首页 > 移动开发 > Android开发

android四大组件之一notification(广播)

2014-12-26 18:17 676 查看
下面来模拟一个点击按钮就可以发送一条消息,并且显示在Title(手机顶部)位置,下面借来看一下代码的具体实现:

1、创建一个点击按钮XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<Button
android:id="@+id/notificationbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送通知"/>

</RelativeLayout>


2、创建一个Activty继承Activty

这里特别住的是要引入一个V4包,

package com.scxh.notification;

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.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
private NotificationCompat.Builder mBuilder;
private NotificationManager mNotifyManager;
private Handler mHandler = new Handler();
private Button mButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton=(Button) findViewById(R.id.notificationbtn);
mButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final int id = 001;
// 第一步
mBuilder = new NotificationCompat.Builder(MainActivity.this);
mBuilder.setSmallIcon(R.drawable.a);
mBuilder.setContentTitle("图片下载");
mBuilder.setContentText("正在下载中...");

//mBuilder.setProgress(0, 0, false);

new Thread(){
public void run() {
int max = 100;
for(int i = 0; i< 100; i++){
mBuilder.setProgress(max, i, false);
mNotifyManager.notify(id, mBuilder.getNotification());

try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
mHandler.post(new Runnable() {

@Override
public void run() {
mBuilder.setContentText("下载完成");
mNotifyManager.notify(id, mBuilder.getNotification());
}
});
};
}.start();

// 第三步 发布通知
Notification notification = mBuilder.getNotification();
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotifyManager.notify(id, notification);
}
});

}

private void createBaseNotification(){
Intent intent=new Intent(MainActivity.this,Zzhuanghuan.class);
//定义notification的Action(行为)
PendingIntent pendingIntentp=PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//获取NotificationManager(通知管理器)
NotificationManager mNotificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建Builder
NotificationCompat.Builder mbBuilder=new NotificationCompat.Builder(MainActivity.this);
mbBuilder.setContentIntent(pendingIntentp);
mbBuilder.setSmallIcon(R.drawable.a);
mbBuilder.setContentTitle("問蒼茫大地,誰主沉浮");
mbBuilder.setContentText("彈指間檣櫓灰飛煙滅");
//弹出显示
mbBuilder.setTicker("問蒼茫大地,誰主沉浮");
//音乐属性
//mbBuilder.setDefaults(Notification.DEFAULT_ALL);

int notificationId=001;
Notification notification= mbBuilder.getNotification();
notification.flags=Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(notificationId,notification);
}
}


这样就可以实现模拟接受通知了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: