消息栏的应用

首先关于布局文件部分:

通知栏显示所用到的布局文件(相对布局)

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" 

    android:padding="5dp">


    <ImageView 

        android:id="@+id/content_view_image"

        android:layout_width="25dp"

        android:layout_height="25dp"

        android:src="@drawable/pear"

        

        />

    <TextView

        android:id="@+id/down_tv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="0%"

        android:textColor="#ffffff"

        android:layout_toRightOf="@id/content_view_image"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="5dp"

        android:layout_marginLeft="15dp"

      />

<ProgressBar 

    android:id="@+id/pb"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        style="@android:style/Widget.ProgressBar.Horizontal"

        android:max="100"

        android:layout_below="@id/content_view_image"

        android:layout_marginTop="4dp"

    />

</RelativeLayout>


代码部分:

类中定义如下:

Notification   notification;

NotificationManager  nm;

Handler  handler = new  Handler();

int  count = 0;

int notification_id=19172439;

Runnable run=null;



在activity的onCreate(Bundle savedInstanceState)方法中,添加如下代码:

nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

        notification=newNotification(R.drawable.pear,"我的香蕉和手表",System.currentTimeMillis());  

      //通知栏显示所用到的布局文件 

        notification.contentView =new RemoteViews(getPackageName(),R.layout.activity_progress);   

        //使用notification.xml文件作VIEW  

       notification.contentView.setProgressBar(R.id.pb, 100,0,false);  

//此处可以用来添加通知栏与activity之间的连接操作

//设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹


run=new Runnable(){  

        

            @Override  

            public void run() {  

                    // TODO Auto-generated method stub          

                    count++;  

                    notification.contentView.setProgressBar(R.id.pb, 100,count,false);

                    notification.contentView.setTextViewText(R.id.down_tv,count+"%");

                    //设置当前值为count  

                    showNotification();//这里是更新notification,就是更新进度条  

                    if(count<100)handler.postDelayed(run, 200);  

                    //200毫秒count加1  

            }  

        };  

下面时启动通知栏:

 nm.notify(notification_id,notification);

//显示notification  

 handler.post(run);



取消通知栏:

nm.cancel(notification_id);



=============================================

点击通知框切换到另一个A  activity需要在上面的提示处添加如下代码:

  Intent notificationIntent = new Intent(this,A.class);   

        PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);   

        notification.contentIntent = contentIntent;      

点击通知框返回正在运行的程序中代码如下:

   Intent appIntent = new Intent(Intent.ACTION_MAIN);

        appIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        appIntent.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + "." + this.getLocalClassName())); 

        appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

        PendingIntent contentIntent = PendingIntent.getActivity(this,0,appIntent,0);   

        notification.contentIntent = contentIntent;  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值