android Notification实践
来源:互联网 发布:摄影灯品牌 知乎 编辑:程序博客网 时间:2024/06/16 23:20
1,先是布局 R.layout.activity_notification
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.halong.myapplication.Notification.NotificationActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="show a default notification." android:textAllCaps="false"/> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="show a custom notification." android:textAllCaps="false"/> </LinearLayout></android.support.constraint.ConstraintLayout>2,NotificationActivity
public class NotificationActivity extends AppCompatActivity implements View.OnClickListener { /** * show a default notification. */ private Button mButton1; /** * show a custom notification. */ private Button mButton2; private NotificationManager mNotificationManager; private NotificationCompat.Builder mDefaultNotificationBuilder; private NotificationCompat.Builder mCustomNotificationBuilder; private RemoteViews mCostomContenView; private BroadcastReceiver mReceiver; private Handler mHandler; private MyThread mThread = null; private int progress = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); initData(); initView(); } @Override protected void onStart() { super.onStart(); //注册BroadcastReceiver IntentFilter filter = new IntentFilter(); filter.addAction("PauseOrContinue"); filter.addAction("Exit"); registerReceiver(mReceiver, filter); //开启ProgressBar更新线程 if (mThread != null && !mThread.isAlive()) { mThread.start(); } } @Override protected void onStop() { //注销BroadcastReceiver unregisterReceiver(mReceiver); //关闭更新线程 if (mThread != null && mThread.isAlive()) { mThread.interrupt(); } super.onStop(); } //以下是自定义方法 private void initData() { mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mDefaultNotificationBuilder = new NotificationCompat.Builder(this); mCustomNotificationBuilder = new NotificationCompat.Builder(this); mCostomContenView = new RemoteViews(getPackageName(), R.layout.layout_notification); mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case "PauseOrContinue": if (mThread.isPause) { mCostomContenView.setTextViewText(R.id.text1, "Pause"); mCustomNotificationBuilder.setCustomContentView(mCostomContenView); mNotificationManager.notify(1, mCustomNotificationBuilder.build()); mThread.goon(); } else { mCostomContenView.setTextViewText(R.id.text1, "Continue"); mCustomNotificationBuilder.setCustomContentView(mCostomContenView); mNotificationManager.notify(1, mCustomNotificationBuilder.build()); mThread.pause(); } break; case "Exit": mThread.pause(); mNotificationManager.cancel(1); break; } } }; mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Log.d("==>", msg.what + ""); switch (msg.what) { case 1: if (progress >= 100) { mThread.pause(); mNotificationManager.cancel(1); } else { progress++; mCostomContenView.setProgressBar(R.id.progress, 100, progress, false); mCustomNotificationBuilder.setCustomContentView(mCostomContenView); mNotificationManager.notify(1, mCustomNotificationBuilder.build()); } break; } } }; mThread = new MyThread(mHandler); } private void initView() { mButton1 = (Button) findViewById(R.id.button1); mButton1.setOnClickListener(this); mButton2 = (Button) findViewById(R.id.button2); mButton2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: showDefaultNotification(); break; case R.id.button2: showCustomNotification(); progress = 0; mThread.goon(); break; } } public void showDefaultNotification() { Intent intent = new Intent(this, NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); mDefaultNotificationBuilder.setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setTicker("this is a default notification.") .setContentTitle("content tittle") .setContentText("this is a default notification.") .setDefaults(Notification.DEFAULT_ALL) .setNumber(1) .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent) .setAutoCancel(true) .addAction(R.mipmap.ic_launcher, "click1", pendingIntent) .addAction(R.mipmap.ic_launcher, "click2", pendingIntent) .setProgress(100, 20, true); mNotificationManager.notify(0, mDefaultNotificationBuilder.build()); } private void showCustomNotification() { mCustomNotificationBuilder.setSmallIcon(R.mipmap.ic_launcher) .setTicker("mCustomNotificationBuilder"); mCostomContenView.setTextViewText(R.id.text1, "Pause"); Intent intent = new Intent("PauseOrContinue"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); mCostomContenView.setOnClickPendingIntent(R.id.text1, pendingIntent); Intent intent2 = new Intent("Exit"); PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 0, intent2, PendingIntent.FLAG_CANCEL_CURRENT); mCostomContenView.setOnClickPendingIntent(R.id.text2, pendingIntent2); mCostomContenView.setProgressBar(R.id.progress, 100, 0, false); mCustomNotificationBuilder.setCustomContentView(mCostomContenView); mCustomNotificationBuilder.setOngoing(true); mNotificationManager.notify(1, mCustomNotificationBuilder.build()); } //时间线程 Thread + Handler用来更新ProgressBar class MyThread extends Thread { Handler mHandler; boolean isStop = false; boolean isPause = true; public MyThread(Handler mHandler) { this.mHandler = mHandler; } @Override public void run() { super.run(); while (!isStop) { if (!isPause) { mHandler.sendEmptyMessage(1); } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public void pause() { isPause = true; } public void goon() { isPause = false; } @Override public void interrupt() { isPause=true; isStop=true; super.interrupt(); } }}
阅读全文
0 0
- android Notification实践
- 消息提示--Notification实践
- ANdroid Notification
- android notification
- android notification
- android notification
- Android Notification
- android notification
- android notification
- android Notification
- Android Notification
- android Notification
- Android notification
- android Notification
- android:Notification
- Android Notification
- Android Notification
- Android Notification
- 炒股笔记之反击线形态
- √n求单值欧拉函数
- ST算法模板题poj3264
- 输入整数,输出最大值和最小值
- SystemUI启动流程及主体布局介绍
- android Notification实践
- java异常详解
- 把两条极坐标曲线变成一条曲线的方法
- 解决SVN在MyEclipse安装后无法显示的问题
- IDEA Cannot resolve symbol 'String' 错误解决
- 电信SMGP协议,基于开源的jar文件smgpapi20100113.jar进行实现
- 如何分析mysql的查询语句
- Python zip函数详解
- java系统高并发解决方案(转载)