博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AndroidのNotification之全方面应用
阅读量:7282 次
发布时间:2019-06-30

本文共 5542 字,大约阅读时间需要 18 分钟。

主Activity

1 package com.test.nitifi; 2  3 import android.app.Activity; 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import android.app.PendingIntent; 7 import android.content.Context; 8 import android.content.Intent; 9 import android.net.Uri;10 import android.os.Bundle;11 import android.util.Log;12 import android.view.View;13 import android.widget.Button;14 15 public class MainActivity extends Activity implements View.OnClickListener{16     /** Called when the activity is first created. */17     int i;18     @Override19     public void onCreate(Bundle savedInstanceState) {20         super.onCreate(savedInstanceState);21         setContentView(R.layout.main);22         Log.e("MainActivity", "onCreate");23         initViews();24     }25     26     void initViews(){27         Button btEnable = (Button)findViewById(R.id.Notifi);28         Button btCancel = (Button)findViewById(R.id.Cancel);29         btEnable.setOnClickListener(this);30         btCancel.setOnClickListener(this);31     }32 33     @Override34     public void onClick(View v) {35         // TODO Auto-generated method stub36         switch(v.getId()){37         case R.id.Notifi:38             Intent intent = new Intent(this,MyService.class);39             if(i%2==0){40                 intent.putExtra("s", true);41             }else{42                 intent.putExtra("s", false);43             }44             startService(intent);45             i++;46             break;47         case R.id.Cancel:48             stopService(new Intent(this,MyService.class));49             50             break;51         }52     }53 54     @Override55     protected void onDestroy() {56         // TODO Auto-generated method stub57         super.onDestroy();58         Log.e("MainActivity", "onDestroy");59     }60 61     @Override62     protected void onResume() {63         // TODO Auto-generated method stub64         super.onResume();65         Log.e("MainActivity", "onResume");66         Log.e("Action", getIntent().getAction());67         68         String is = this.getIntent().getStringExtra("interval");69         if(is!=null){70             Log.e("MainActivity", "有");71         }else{72             Log.e("MainActivity", "没有");73         }74     }75 76     @Override77     protected void onStart() {78         // TODO Auto-generated method stub79         super.onStart();80         Log.e("MainActivity", "onStart");81     }82 83     @Override84     protected void onStop() {85         // TODO Auto-generated method stub86         super.onStop();87         Log.e("MainActivity", "onStop");88     }89     90     91 }

 

 

1 package com.test.nitifi; 2  3 import android.app.Notification; 4 import android.app.NotificationManager; 5 import android.app.PendingIntent; 6 import android.app.Service; 7 import android.content.Context; 8 import android.content.Intent; 9 import android.net.Uri;10 import android.os.IBinder;11 import android.util.Log;12 13 public class MyService extends Service{14 15     NotificationManager nm;16     Notification nitifi;17     PendingIntent pi;18     Intent intent;19     20     @Override21     public IBinder onBind(Intent arg0) {22         // TODO Auto-generated method stub23         return null;24     }25 26     @Override27     public void onCreate() {28         // TODO Auto-generated method stub29         super.onCreate();30         Log.e("MyService", "onCreate");31         nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);32         intent = new Intent(this,MyReceiver.class);33         intent.setAction("act");34         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);35         intent.putExtra("wo", "buxing");36         intent.putExtra("nitifiId", 0);37         pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);38         nitifi = new Notification(R.drawable.ic_launcher,"新订单",System.currentTimeMillis());39         nitifi.setLatestEventInfo(this, "点击可查看", "2015645343454", pi);40         nitifi.sound = Uri.parse("android.resource://"+this.getPackageName()+"/"+R.raw.neworder);41     }42 43     @Override44     public void onDestroy() {45         // TODO Auto-generated method stub46         super.onDestroy();47         Log.e("MyService", "onDestroy");48         nm.cancel(0);49     }50 51     @Override52     public void onStart(Intent intent, int startId) {53         // TODO Auto-generated method stub54         super.onStart(intent, startId);55         Log.e("MyService", "onStart");56         if(intent.getBooleanExtra("s", false)){57             Log.e("intent", "偶数");58             nitifi.icon = R.drawable.ic_launcher;59         }else{60             Log.e("intent", "奇数");61             nitifi.icon = R.drawable.me;62         }63         64         nm.notify(0, nitifi);65     }66 67     68     69 }

 

 

package com.test.nitifi;import android.app.NotificationManager;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiver extends BroadcastReceiver{    NotificationManager nm;    @Override    public void onReceive(Context context, Intent intent) {        // TODO Auto-generated method stub        Log.e("MyReceiver", intent.getAction());        nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);        nm.cancel(intent.getIntExtra("nitifiId", 0));        Intent it = new Intent(context,MainActivity.class);        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        context.startActivity(it);    }        }

转载地址:http://rvzjm.baihongyu.com/

你可能感兴趣的文章
《程序猿的生命周期》阅读有感
查看>>
由浅入深的来聊聊Golang中select的实现机制
查看>>
10 Java NIO ServerSocketChannel-翻译
查看>>
apache配置修改
查看>>
国内Rubygem镜像
查看>>
swift - self的弱引用
查看>>
Web技巧(10)
查看>>
转载:定位ARM Hard Fault 的方法
查看>>
第九周周记
查看>>
VSCode利用Snippets设置超实用的代码块
查看>>
画解算法:14. 最长公共前缀
查看>>
【51NOD-0】1130 N的阶乘的长度 V2(斯特林近似)
查看>>
Leet Code OJ 简单(一)
查看>>
Es6 - 字符串模板
查看>>
Nginx 配置 Jenkins 反向代理
查看>>
Codeforces Round #284 (Div. 2) D. Name That Tune [概率dp]
查看>>
QQ 的登录封面是怎么设计的
查看>>
拯救不会函数的我!!
查看>>
cocos2dx 用命令行进行编译的指令
查看>>
NOIP 合唱队形
查看>>