Notification 模拟收到短信,数据下载的状态栏提示
生活随笔
收集整理的這篇文章主要介紹了
Notification 模拟收到短信,数据下载的状态栏提示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Notification 模擬收到短信
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="單擊發送Notification信息"/> </LinearLayout>
功能代碼實現
第二:定義消息通知的布局,進度條顯示
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><!--定義通知布局的文本框--><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="下載中……"android:textSize="20sp"android:textColor="@android:color/white"/><!--定義下載進度progressbar控件--><ProgressBarandroid:id="@+id/pb"style="?android:attr/progressBarStyleHorizontal"android:layout_width="260dp"android:layout_height="wrap_content"android:layout_gravity="center_vertical"/> </LinearLayout>
監聽按鈕
得到NotificationManager的服務對象
初始化并得到Notification的視圖對象,設置progressBar對象
定義單擊通知事件。取消事件
package com.ncsyeyy.YeyyNotificationDownload;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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RemoteViews;public class MyActivity extends Activity implements OnClickListener { // 定義notification的idprivate int notification_id=1; // 定義主線程的handlerprivate Handler handler=new Handler() ; // 記錄進度條進度private int count=0; // 記錄是否進度條取消private Boolean isclean=false;private Button btnSend;private Button btnClean;private NotificationManager nm;private Notification notification;/*** Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findView();setClick(); // 得到NotificationManager的服務對象nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // 初始化notification對象notification = new Notification(R.drawable.ic_launcher,"開始下載",System.currentTimeMillis()); // 得到Notification的視圖對象notification.contentView=new RemoteViews(getPackageName(),R.layout.layout_notification); // 設置視圖中的ProgressBar對象notification.contentView.setProgressBar(R.id.pb,100,0,false); // 定義單擊通知事件Intent notificationIntent=new Intent(this,MyActivity.class);PendingIntent contentIntent= PendingIntent.getActivity(this,0,notificationIntent,0);notification.contentIntent=contentIntent;}private void findView(){btnSend = (Button) findViewById(R.id.btnSend);btnClean = (Button) findViewById(R.id.btnClean);}private void setClick(){btnSend.setOnClickListener(this);btnClean.setOnClickListener(this);}@Overridepublic void onClick(View v) { // 自定義按鈕單擊監聽器switch (v.getId()){case R.id.btnClean: // 取消notificationnm.cancel(notification_id);isclean=true;break;case R.id.btnSend: // 顯示notificationshowNotification();handler.post(run);break;default:break;}}// 定義Runnable對象進行進度更新Runnable run=new Runnable() {@Overridepublic void run() { // 判斷通知是否被取消if (!isclean){ // 如果沒有取消就進行進度的更新count++;notification.contentView.setProgressBar(R.id.pb,100,count,false); // 200毫秒count加1if (count<100)handler.postDelayed(run,200);}} }; // 顯示notificationpublic void showNotification(){nm.notify(notification_id,notification);} }
? ? ? ??
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="單擊發送Notification信息"/> </LinearLayout>
功能代碼實現
package com.ncsyeyy.YeyyNotificaton;import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;public class MyActivity extends Activity implements OnClickListener {private Button btn;/*** Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findView();setOnClick();}private void findView(){btn = (Button) findViewById(R.id.btn);}private void setOnClick(){btn.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.btn:sendNotification();break;}}private void sendNotification(){ // 得到系統的Notification服務對象NotificationManager manager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); // 創建一個Notification對象圖標Notification notification=new Notification(); // 設置顯示的Notification對象圖標notification.icon=R.drawable.ic_launcher; // 設置顯示Notification對象的內容notification.tickerText="您有一條新的短信!"; // 設置顯示Notification對象的聲音模式notification.audioStreamType= AudioManager.ADJUST_LOWER; // 定義單擊Notification的事件IntentIntent intent=new Intent(this,MyActivity.class);PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);// 單擊狀態欄的圖標出現的提示信息設置notification.setLatestEventInfo(this,"短信提示內容","我是一個短消息,愚人節快樂!",pendingIntent); // 發送pendingIntent消息manager.notify(1,notification);}}
思路:
第一:定義布局按鈕
第二:定義消息通知的布局,進度條顯示
第三:代碼實現
監聽按鈕
得到NotificationManager的服務對象
初始化并得到Notification的視圖對象,設置progressBar對象
定義單擊通知事件。取消事件
第一:定義布局按鈕
第二:定義消息通知的布局,進度條顯示
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><!--定義通知布局的文本框--><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="下載中……"android:textSize="20sp"android:textColor="@android:color/white"/><!--定義下載進度progressbar控件--><ProgressBarandroid:id="@+id/pb"style="?android:attr/progressBarStyleHorizontal"android:layout_width="260dp"android:layout_height="wrap_content"android:layout_gravity="center_vertical"/> </LinearLayout>
監聽按鈕
得到NotificationManager的服務對象
初始化并得到Notification的視圖對象,設置progressBar對象
定義單擊通知事件。取消事件
package com.ncsyeyy.YeyyNotificationDownload;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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RemoteViews;public class MyActivity extends Activity implements OnClickListener { // 定義notification的idprivate int notification_id=1; // 定義主線程的handlerprivate Handler handler=new Handler() ; // 記錄進度條進度private int count=0; // 記錄是否進度條取消private Boolean isclean=false;private Button btnSend;private Button btnClean;private NotificationManager nm;private Notification notification;/*** Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findView();setClick(); // 得到NotificationManager的服務對象nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // 初始化notification對象notification = new Notification(R.drawable.ic_launcher,"開始下載",System.currentTimeMillis()); // 得到Notification的視圖對象notification.contentView=new RemoteViews(getPackageName(),R.layout.layout_notification); // 設置視圖中的ProgressBar對象notification.contentView.setProgressBar(R.id.pb,100,0,false); // 定義單擊通知事件Intent notificationIntent=new Intent(this,MyActivity.class);PendingIntent contentIntent= PendingIntent.getActivity(this,0,notificationIntent,0);notification.contentIntent=contentIntent;}private void findView(){btnSend = (Button) findViewById(R.id.btnSend);btnClean = (Button) findViewById(R.id.btnClean);}private void setClick(){btnSend.setOnClickListener(this);btnClean.setOnClickListener(this);}@Overridepublic void onClick(View v) { // 自定義按鈕單擊監聽器switch (v.getId()){case R.id.btnClean: // 取消notificationnm.cancel(notification_id);isclean=true;break;case R.id.btnSend: // 顯示notificationshowNotification();handler.post(run);break;default:break;}}// 定義Runnable對象進行進度更新Runnable run=new Runnable() {@Overridepublic void run() { // 判斷通知是否被取消if (!isclean){ // 如果沒有取消就進行進度的更新count++;notification.contentView.setProgressBar(R.id.pb,100,count,false); // 200毫秒count加1if (count<100)handler.postDelayed(run,200);}} }; // 顯示notificationpublic void showNotification(){nm.notify(notification_id,notification);} }
? ? ? ??
源碼地址:http://download.csdn.net/detail/csdnyuandaimaxuexi/9214261
總結
以上是生活随笔為你收集整理的Notification 模拟收到短信,数据下载的状态栏提示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言的整型常量与实型常量
- 下一篇: 电脑突然蓝屏要怎么办?电脑为啥会蓝屏?