android四大组件之一notification(广播)
2014-12-26 18:17
676 查看
下面来模拟一个点击按钮就可以发送一条消息,并且显示在Title(手机顶部)位置,下面借来看一下代码的具体实现:
1、创建一个点击按钮XML布局
2、创建一个Activty继承Activty
这里特别住的是要引入一个V4包,
这样就可以实现模拟接受通知了
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); } }
这样就可以实现模拟接受通知了
相关文章推荐
- android四大组件之一广播notification(通知)的Title模拟下载
- ANDROID笔记:四大组件之广播
- android开发步步为营之38:四大组件之BroadcastReceiver接收广播
- 【Android学习之道】 四大组件之广播接收器Broadcast Receiver
- Android 四大组件 —— 广播(广播机制解析)
- Android四大组件之广播接收器 Broadcast Receiver
- ANDROID笔记:四大组件之广播
- 关于Android四大组件之一broadcast receiver的详解(广播接收者)
- android四大组件之Service 注册广播接收者
- Android四大组件之Broadcast(广播接收BroadcastReceiver)
- Android 四大组件之(3)BroadcastReceiver广播事件处理以及与Notification的配合使用详解
- Android四大组件之广播接收者
- Android四大组件之广播接收者
- android四大组件之广播接收器BroadcastReceiver
- Android四大组件之广播的使用
- Android四大组件之一:BroadCastReceiver(广播接收者)
- Android四大组件之广播接收者
- Android四大组件----广播发送者与广播接收者的使用
- Android开发-四大组件之服务、广播
- Android四大组件之广播接收者