Android之手机通知栏Notification的使用(二)

来源:互联网 发布:南风知我意温南txt 编辑:程序博客网 时间:2024/05/21 17:13

先看效果图吧,可能有点小缺陷尴尬

怎么取消通知都取消不了,有哪位大神可以给我解释一下啊大哭


MainActivity:

package com.example.notificationapp;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.content.res.Resources.NotFoundException;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.view.Menu;import android.view.View;import android.widget.RemoteViews;import android.widget.RemoteViews.RemoteView;public class MainActivity extends Activity {private int num;private Notification.Builder  builder;//获取Notification.Builder对象private Notification notify;//创建Notification对象private NotificationManager nm;//获取NotificationManager对象//发送通知public void mySet(View view) {// TODO Auto-generated method stub//创建内部类对象builder=new Notification.Builder(this);//设置通知的属性builder.setSmallIcon(R.drawable.ic_launcher);builder.setContentText("恭喜你中奖了!!中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了中奖了");builder.setContentTitle("新消息");//大图标Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.domob_exit_on);builder.setLargeIcon(b);//设置通知的消息builder.setContentInfo("0%");//提示有消息来了builder.setTicker("新通知新通知");//设置时间为系统时间builder.setWhen(System.currentTimeMillis());//设置通知的点击事件Intent intent=new Intent(this,OtherActivity.class);PendingIntent pi=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(pi);//设置通知点击之后自动取消builder.setAutoCancel(true);/*//设置默认提示音builder.setDefaults(Notification.DEFAULT_SOUND);*///设置自定义声音builder.setSound(Uri.parse("file:///sdcard/apple.mp3"));    //设置自定义的震动builder.setVibrate(new long[]{1000,3000,2000,3000});//设置自定义的闪屏builder.setLights(1000, 3000, 5000);//设置进度样式条的通知/*builder.setProgress(0, 0, true);*/builder.setProgress(100,num, false);//创建通知对象notify=builder.build();//第一个参数是标识符,表明是哪个nm.notify(10, notify);//用子线程更新进度条new Thread(){public void run() {while(num<=100){try {sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}num++;Message msg=new Message();msg.arg1=num;msg.what=0x123;handler.sendMessage(msg);}}}.start();}private Handler handler=new Handler(){public void handleMessage(Message msg) {if(msg.what==0x123){builder.setProgress(100, msg.arg1, false);builder.setContentInfo(num+"%");notify=builder.build();nm.notify(10,notify);}}};//取消通知public void myCancel(View view) {// TODO Auto-generated method stubnm.cancel(10);}//发送大视图通知public void mybig(View view) {// TODO Auto-generated method stubNotification notify=new Notification.Builder(this).setTicker("新通知新通知").setContentTitle("新闻").setSmallIcon(R.drawable.ic_launcher).setContentInfo("888").setStyle(new Notification.InboxStyle().addLine("中奖了!!!!!").addLine("你来了啊啊").addLine("我来了哈哈").addLine("来吧来吧").setSummaryText("www.baidu.com")).build();nm.notify(11, notify);}//自定义View通知public void myview(View view) {// TODO Auto-generated method stub//第一个参数为包的名称,第二个为layout资源的Id。RemoteViews v =new RemoteViews(getPackageName(), R.layout.activity_view);    v.setTextViewText(R.id.text, "今日头条");     v.setImageViewResource(R.id.img, R.drawable.domob_exit_on);Notification notify=new Notification.Builder(this)      .setSmallIcon(R.drawable.domob_exit_on)      .setTicker("新通知新通知")      .setContent(v)      .build();      nm.notify(12,notify);     }@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);setContentView(R.layout.activity_main);    }}


布局文件就不贴了,就几个Button。。

想看源代码的这里


















0 0
原创粉丝点击