Notification 模拟收到短信,数据下载的状态栏提示

来源:互联网 发布:大数据时代与法治政府 编辑:程序博客网 时间:2024/04/29 18:43
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"        >    <Button            android: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.     */    @Override    public 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);    }    @Override    public 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的事件Intent        Intent 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);    }}


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="fill_parent"              android:layout_height="fill_parent"        >    <Button            android:id="@+id/btnSend"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="发送下载Notification"            />    <Button            android:id="@+id/btnClean"            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">    <!--定义通知布局的文本框-->    <TextView            android:id="@+id/tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="下载中……"            android:textSize="20sp"            android:textColor="@android:color/white"            />    <!--定义下载进度progressbar控件-->    <ProgressBar            android: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的id    private int notification_id=1;//    定义主线程的handler    private 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.     */    @Override    public 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);    }    @Override    public void onClick(View v) {//        自定义按钮单击监听器        switch (v.getId()){            case R.id.btnClean://                取消notification                nm.cancel(notification_id);                isclean=true;                break;            case R.id.btnSend://                显示notification                showNotification();                handler.post(run);                break;            default:                break;        }    }//    定义Runnable对象进行进度更新    Runnable run=new Runnable() {    @Override    public void run() {//        判断通知是否被取消        if (!isclean){//            如果没有取消就进行进度的更新            count++;            notification.contentView.setProgressBar(R.id.pb,100,count,false);//            200毫秒count加1            if (count<100)                handler.postDelayed(run,200);        }    }};//    显示notification    public void showNotification(){        nm.notify(notification_id,notification);    }}

        


源码地址:http://download.csdn.net/detail/csdnyuandaimaxuexi/9214261


0 0