android Notification实践

1,先是布局  R.layout.activity_notification

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="com.example.halong.myapplication.Notification.NotificationActivity">  
  8.     <LinearLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:orientation="vertical">  
  12.   
  13.         <Button  
  14.             android:id="@+id/button1"  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="show a default notification."  
  18.             android:textAllCaps="false"/>  
  19.         <Button  
  20.             android:id="@+id/button2"  
  21.             android:layout_width="match_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="show a custom notification."  
  24.             android:textAllCaps="false"/>  
  25.   
  26.     </LinearLayout>  
  27.   
  28. </android.support.constraint.ConstraintLayout>  
2, NotificationActivity

[java]  view plain  copy
  1. public class NotificationActivity extends AppCompatActivity implements View.OnClickListener {  
  2.   
  3.   
  4.     /** 
  5.      * show a default notification. 
  6.      */  
  7.     private Button mButton1;  
  8.     /** 
  9.      * show a custom notification. 
  10.      */  
  11.     private Button mButton2;  
  12.   
  13.     private NotificationManager mNotificationManager;  
  14.     private NotificationCompat.Builder mDefaultNotificationBuilder;  
  15.     private NotificationCompat.Builder mCustomNotificationBuilder;  
  16.     private RemoteViews mCostomContenView;  
  17.   
  18.     private BroadcastReceiver mReceiver;  
  19.     private Handler mHandler;  
  20.     private MyThread mThread = null;  
  21.   
  22.     private int progress = 0;  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_notification);  
  28.   
  29.         initData();  
  30.         initView();  
  31.     }  
  32.   
  33.     @Override  
  34.     protected void onStart() {  
  35.         super.onStart();  
  36.   
  37.         //注册BroadcastReceiver  
  38.         IntentFilter filter = new IntentFilter();  
  39.         filter.addAction("PauseOrContinue");  
  40.         filter.addAction("Exit");  
  41.         registerReceiver(mReceiver, filter);  
  42.   
  43.         //开启ProgressBar更新线程  
  44.         if (mThread != null && !mThread.isAlive()) {  
  45.             mThread.start();  
  46.         }  
  47.     }  
  48.   
  49.     @Override  
  50.     protected void onStop() {  
  51.         //注销BroadcastReceiver  
  52.         unregisterReceiver(mReceiver);  
  53.   
  54.         //关闭更新线程  
  55.         if (mThread != null && mThread.isAlive()) {  
  56.             mThread.interrupt();  
  57.         }  
  58.         super.onStop();  
  59.     }  
  60.   
  61.     //以下是自定义方法  
  62.   
  63.     private void initData() {  
  64.         mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  65.         mDefaultNotificationBuilder = new NotificationCompat.Builder(this);  
  66.         mCustomNotificationBuilder = new NotificationCompat.Builder(this);  
  67.         mCostomContenView = new RemoteViews(getPackageName(), R.layout.layout_notification);  
  68.   
  69.         mReceiver = new BroadcastReceiver() {  
  70.             @Override  
  71.             public void onReceive(Context context, Intent intent) {  
  72.                 switch (intent.getAction()) {  
  73.                     case "PauseOrContinue":  
  74.                         if (mThread.isPause) {  
  75.                             mCostomContenView.setTextViewText(R.id.text1, "Pause");  
  76.                             mCustomNotificationBuilder.setCustomContentView(mCostomContenView);  
  77.                             mNotificationManager.notify(1, mCustomNotificationBuilder.build());  
  78.                             mThread.goon();  
  79.                         } else {  
  80.                             mCostomContenView.setTextViewText(R.id.text1, "Continue");  
  81.                             mCustomNotificationBuilder.setCustomContentView(mCostomContenView);  
  82.                             mNotificationManager.notify(1, mCustomNotificationBuilder.build());  
  83.                             mThread.pause();  
  84.                         }  
  85.                         break;  
  86.   
  87.                     case "Exit":  
  88.                         mThread.pause();  
  89.                         mNotificationManager.cancel(1);  
  90.                         break;  
  91.                 }  
  92.   
  93.             }  
  94.         };  
  95.   
  96.         mHandler = new Handler() {  
  97.             @Override  
  98.             public void handleMessage(Message msg) {  
  99.                 super.handleMessage(msg);  
  100.   
  101.                 Log.d("==>", msg.what + "");  
  102.                 switch (msg.what) {  
  103.                     case 1:  
  104.                         if (progress >= 100) {  
  105.                             mThread.pause();  
  106.                             mNotificationManager.cancel(1);  
  107.                         } else {  
  108.                             progress++;  
  109.                             mCostomContenView.setProgressBar(R.id.progress, 100, progress, false);  
  110.                             mCustomNotificationBuilder.setCustomContentView(mCostomContenView);  
  111.                             mNotificationManager.notify(1, mCustomNotificationBuilder.build());  
  112.                         }  
  113.                         break;  
  114.   
  115.                 }  
  116.   
  117.             }  
  118.         };  
  119.   
  120.         mThread = new MyThread(mHandler);  
  121.     }  
  122.   
  123.     private void initView() {  
  124.         mButton1 = (Button) findViewById(R.id.button1);  
  125.         mButton1.setOnClickListener(this);  
  126.         mButton2 = (Button) findViewById(R.id.button2);  
  127.         mButton2.setOnClickListener(this);  
  128.     }  
  129.   
  130.     @Override  
  131.     public void onClick(View v) {  
  132.         switch (v.getId()) {  
  133.             case R.id.button1:  
  134.                 showDefaultNotification();  
  135.                 break;  
  136.             case R.id.button2:  
  137.                 showCustomNotification();  
  138.                 progress = 0;  
  139.                 mThread.goon();  
  140.                 break;  
  141.         }  
  142.     }  
  143.   
  144.     public void showDefaultNotification() {  
  145.         Intent intent = new Intent(this, NotificationActivity.class);  
  146.         PendingIntent pendingIntent = PendingIntent.getActivity(this0, intent, PendingIntent.FLAG_CANCEL_CURRENT);  
  147.   
  148.         mDefaultNotificationBuilder.setSmallIcon(R.mipmap.ic_launcher)  
  149.                 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))  
  150.                 .setTicker("this is a default notification.")  
  151.                 .setContentTitle("content tittle")  
  152.                 .setContentText("this is a default notification.")  
  153.                 .setDefaults(Notification.DEFAULT_ALL)  
  154.                 .setNumber(1)  
  155.                 .setWhen(System.currentTimeMillis())  
  156.                 .setContentIntent(pendingIntent)  
  157.                 .setAutoCancel(true)  
  158.                 .addAction(R.mipmap.ic_launcher, "click1", pendingIntent)  
  159.                 .addAction(R.mipmap.ic_launcher, "click2", pendingIntent)  
  160.                 .setProgress(10020true);  
  161.   
  162.         mNotificationManager.notify(0, mDefaultNotificationBuilder.build());  
  163.     }  
  164.   
  165.   
  166.     private void showCustomNotification() {  
  167.         mCustomNotificationBuilder.setSmallIcon(R.mipmap.ic_launcher)  
  168.                 .setTicker("mCustomNotificationBuilder");  
  169.   
  170.         mCostomContenView.setTextViewText(R.id.text1, "Pause");  
  171.   
  172.         Intent intent = new Intent("PauseOrContinue");  
  173.         PendingIntent pendingIntent = PendingIntent.getBroadcast(this0, intent, PendingIntent.FLAG_CANCEL_CURRENT);  
  174.         mCostomContenView.setOnClickPendingIntent(R.id.text1, pendingIntent);  
  175.   
  176.         Intent intent2 = new Intent("Exit");  
  177.         PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this0, intent2, PendingIntent.FLAG_CANCEL_CURRENT);  
  178.         mCostomContenView.setOnClickPendingIntent(R.id.text2, pendingIntent2);  
  179.   
  180.         mCostomContenView.setProgressBar(R.id.progress, 1000false);  
  181.   
  182.         mCustomNotificationBuilder.setCustomContentView(mCostomContenView);  
  183.         mCustomNotificationBuilder.setOngoing(true);  
  184.         mNotificationManager.notify(1, mCustomNotificationBuilder.build());  
  185.     }  
  186.   
  187.   
  188.     //时间线程  Thread + Handler用来更新ProgressBar  
  189.     class MyThread extends Thread {  
  190.         Handler mHandler;  
  191.         boolean isStop = false;  
  192.         boolean isPause = true;  
  193.   
  194.         public MyThread(Handler mHandler) {  
  195.             this.mHandler = mHandler;  
  196.         }  
  197.   
  198.         @Override  
  199.         public void run() {  
  200.             super.run();  
  201.             while (!isStop) {  
  202.                 if (!isPause) {  
  203.                     mHandler.sendEmptyMessage(1);  
  204.                 }  
  205.                 try {  
  206.                     Thread.sleep(100);  
  207.                 } catch (InterruptedException e) {  
  208.                     e.printStackTrace();  
  209.                 }  
  210.             }  
  211.         }  
  212.   
  213.         public void pause() {  
  214.             isPause = true;  
  215.         }  
  216.   
  217.         public void goon() {  
  218.             isPause = false;  
  219.         }  
  220.   
  221.         @Override  
  222.         public void interrupt() {  
  223.             isPause=true;  
  224.             isStop=true;  
  225.             super.interrupt();  
  226.         }  
  227.     }  

宁波鼻部整形:http://www.iyestar.com/bbzx/
  1. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值