背景:新年之际,微信微博支付宝红包是到处飞,但是,自己的手速总是比别人慢一点最后导致红包没抢到,红包助手就应运而生。

需求:收到红包的时候进行提醒,然后跳转到红包的界面方便用户

思路:获取“读取通知信息”权限,然后开启服务监控系统通知,判断如果是微信红包就进行提醒(声音),然后跳转到红包所在的地方

界面:

界面分为两部分,一部分是可以对App进行操作的,下面是一个可以滑动的界面,提示用户如何是软件正常工作,布局代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. android:id="@+id/root"
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:layout_marginLeft="10dp"
  9. android:layout_marginRight="10dp"
  10. android:layout_marginTop="5dp"
  11. android:orientation="vertical"
  12. tools:context="com.fndroid.administrator.justforyou.MainActivity">
  13.  
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:orientation="horizontal">
  18.  
  19. <TextView
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_weight="3"
  23. android:text="打开提示音"/>
  24.  
  25. <CheckBox
  26. android:id="@+id/isMusic"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"/>
  29.  
  30. </LinearLayout>
  31.  
  32. <LinearLayout
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content">
  35.  
  36. <TextView
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_gravity="center"
  40. android:text="音量调节"/>
  41.  
  42. <SeekBar
  43. android:id="@+id/seekbar"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_gravity="center"
  47. android:layout_weight="1"/>
  48.  
  49. </LinearLayout>
  50.  
  51. <LinearLayout
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:orientation="horizontal">
  55.  
  56. <TextView
  57. android:layout_width="0dp"
  58. android:layout_height="wrap_content"
  59. android:layout_weight="3"
  60. android:text="有红包亮屏并解锁"/>
  61.  
  62. <CheckBox
  63. android:id="@+id/isUnlock"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"/>
  66. </LinearLayout>
  67.  
  68. <Button
  69. android:id="@+id/setPermision"
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:layout_gravity="center"
  73. android:text="设置通知权限"/>
  74.  
  75. <ScrollView
  76. android:layout_width="match_parent"
  77. android:layout_height="match_parent">
  78.  
  79. <LinearLayout
  80. android:layout_width="match_parent"
  81. android:layout_height="match_parent"
  82. android:orientation="vertical">
  83.  
  84. <TextView
  85. android:layout_width="wrap_content"
  86. android:layout_height="wrap_content"
  87. android:layout_marginTop="10dp"
  88. android:text="声明:"/>
  89.  
  90. <TextView
  91. android:layout_width="wrap_content"
  92. android:layout_height="wrap_content"
  93. android:text="本软件为个人开发所得,只能对微信红包进行提醒。请合理使用本软件,使用不当造成的各种行为均与本人无关。软件使用过程不联网,不存在任何盗窃用户信息行为,请放心使用。"/>
  94.  
  95. <TextView
  96. android:layout_width="wrap_content"
  97. android:layout_height="wrap_content"
  98. android:layout_marginTop="10dp"
  99. android:text="使用方法:"/>
  100.  
  101. <TextView
  102. android:layout_width="wrap_content"
  103. android:layout_height="wrap_content"
  104. android:text="①如果未赋予软件读取通知权限,点击按钮“设置通知权限"/>
  105.  
  106. <TextView
  107. android:layout_width="wrap_content"
  108. android:layout_height="wrap_content"
  109. android:text="②在本软件右侧勾选上,并确认提示信息"/>
  110.  
  111. <ImageView
  112. android:layout_width="wrap_content"
  113. android:layout_height="wrap_content"
  114. android:scaleType="fitCenter"
  115. android:src="@drawable/inf"/>
  116.  
  117. <TextView
  118. android:layout_width="wrap_content"
  119. android:layout_height="wrap_content"
  120. android:text="③关闭微信群的消息免打扰(取消图中的绿色按钮)"/>
  121.  
  122. <ImageView
  123. android:layout_width="wrap_content"
  124. android:layout_height="wrap_content"
  125. android:src="@drawable/inf2"/>
  126. </LinearLayout>
  127. </ScrollView>
  128. </LinearLayout>

app打开的时候开启一个服务,编写一个NotificationListenerService的子类并实现onNotificationPosted和onNotificationRemoved方法,前面的方法会在收到通知的时候调用

  1. // 编写一个NotificationListenerService的子类并实现onNotificationPosted和onNotificationRemoved方法
  2. // 这两个方法在从SDK版本21的时候开始变成了非抽象,不重写则不能兼容21以下设备
  3. public class NotificationService extends NotificationListenerService {
  4.  
  5. private KeyguardManager.KeyguardLock kl;
  6.  
  7. @Override
  8. public void onNotificationPosted(StatusBarNotification sbn) {
  9. // 主界面设置的信息保存在SharedPreferences中,在这里进行获取
  10. SharedPreferences sharedPreferences = getSharedPreferences("userdata", MODE_PRIVATE);
  11.  
  12. // 判断消息是否为微信红包
  13. if (sbn.getNotification().tickerText.toString().contains("[微信红包]") && sbn.getPackageName
  14. ().equals("com.tencent.mm")) {
  15.  
  16. // 读取设置信息,判断是否该点亮屏幕并解开锁屏,解锁的原理是把锁屏关闭掉
  17. if (sharedPreferences.getBoolean("isUnlock",true)) {
  18. KeyguardManager km = (KeyguardManager) getSystemService(getApplicationContext()
  19. .KEYGUARD_SERVICE);
  20. kl = km.newKeyguardLock("unlock");
  21.  
  22. // 把系统锁屏暂时关闭
  23. kl.disableKeyguard();
  24. PowerManager pm = (PowerManager) getSystemService(getApplicationContext()
  25. .POWER_SERVICE);
  26. PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
  27. PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
  28. wl.acquire();
  29. wl.release();
  30. }
  31.  
  32. try {
  33. // 打开notification所对应的pendingintent
  34. sbn.getNotification().contentIntent.send();
  35.  
  36. } catch (PendingIntent.CanceledException e) {
  37. e.printStackTrace();
  38. }
  39.  
  40. // 判断是否该播放提示音
  41. if (sharedPreferences.getBoolean("isMusic",true)){
  42. MediaPlayer mediaPlayer = new MediaPlayer().create(this, R.raw.heihei);
  43. mediaPlayer.start();
  44. }
  45.  
  46. // 这里监听一下系统广播,判断如果屏幕熄灭就把系统锁屏还原
  47. IntentFilter intentFilter = new IntentFilter();
  48. intentFilter.addAction("android.intent.action.SCREEN_OFF");
  49. ScreenOffReceiver screenOffReceiver = new ScreenOffReceiver();
  50. registerReceiver(screenOffReceiver, intentFilter);
  51.  
  52. }
  53.  
  54. }
  55.  
  56. class ScreenOffReceiver extends BroadcastReceiver {
  57. @Override
  58. public void onReceive(Context context, Intent intent) {
  59. if (kl != null) {
  60. // 还原锁屏
  61. kl.reenableKeyguard();
  62. }
  63. }
  64. }
  65.  
  66. @Override
  67. public void onNotificationRemoved(StatusBarNotification sbn) {
  68. super.onNotificationRemoved(sbn);
  69. }
  70. }

主的activity,注释在代码中了,就不详细说了

  1. public class MainActivity extends AppCompatActivity implements CompoundButton
  2. .OnCheckedChangeListener, View.OnClickListener,SeekBar.OnSeekBarChangeListener {
  3.  
  4. private LinearLayout root;
  5. private CheckBox isMusic;
  6. private CheckBox isUnlock;
  7. private SharedPreferences.Editor editor;
  8. private SharedPreferences sharedPreferences;
  9. private Button setPermision;
  10. private SeekBar seekBar;
  11. private AudioManager audioManager;
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17.  
  18. // 获取控件实例
  19. root = (LinearLayout) findViewById(R.id.root);
  20. isMusic = (CheckBox) findViewById(R.id.isMusic);
  21. isUnlock = (CheckBox) findViewById(R.id.isUnlock);
  22. setPermision = (Button) findViewById(R.id.setPermision);
  23. seekBar = (SeekBar) findViewById(R.id.seekbar);
  24.  
  25. // 注册监听
  26. isMusic.setOnCheckedChangeListener(this);
  27. isUnlock.setOnCheckedChangeListener(this);
  28. setPermision.setOnClickListener(this);
  29. seekBar.setOnSeekBarChangeListener(this);
  30.  
  31. // 读取设置信息
  32. sharedPreferences = getSharedPreferences("userdata", MODE_PRIVATE);
  33. editor = sharedPreferences.edit();
  34. boolean music = sharedPreferences.getBoolean("isMusic", true);
  35. boolean unlock = sharedPreferences.getBoolean("isUnlock", true);
  36. isMusic.setChecked(music);
  37. isUnlock.setChecked(unlock);
  38.  
  39. // 获得Audiomanager,控制系统音量
  40. audioManager = (AudioManager) getSystemService(this.AUDIO_SERVICE);
  41. seekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
  42. seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
  43.  
  44. // 监听系统媒体音量改变,并改变界面上的Seekbar的进度
  45. IntentFilter intentFilter = new IntentFilter();
  46. intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
  47. VolumReceiver receiver = new VolumReceiver();
  48. registerReceiver(receiver,intentFilter);
  49.  
  50. // 开启服务
  51. Intent intent = new Intent(MainActivity.this, NotificationService.class);
  52. startService(intent);
  53. }
  54.  
  55. @Override
  56. public boolean onKeyDown(int keyCode, KeyEvent event) {
  57. // 判断返回键点击,提示用户是否确认退出
  58. if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
  59. Snackbar snackbar = Snackbar.make(root, "退出软件", Snackbar.LENGTH_LONG)
  60. .setAction("确认", new View.OnClickListener() {
  61. @Override
  62. public void onClick(View v) {
  63. MainActivity.this.finish();
  64. }
  65. });
  66. snackbar.show();
  67. return true;
  68. }
  69. return super.onKeyDown(keyCode, event);
  70. }
  71.  
  72. @Override
  73. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  74. // checkbox的点击监听
  75. switch (buttonView.getId()) {
  76. case R.id.isMusic:
  77. editor.putBoolean("isMusic", isChecked);
  78. editor.commit();
  79. break;
  80. case R.id.isUnlock:
  81. editor.putBoolean("isUnlock", isChecked);
  82. editor.commit();
  83. break;
  84. }
  85.  
  86. }
  87.  
  88. @Override
  89. public void onClick(View v) {
  90. switch (v.getId()){
  91. case R.id.setPermision:
  92. // 打开系统里面的服务,方便用户直接赋予权限
  93. Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
  94. startActivity(intent);
  95. break;
  96. }
  97.  
  98. }
  99.  
  100. @Override
  101. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  102. }
  103.  
  104. @Override
  105. public void onStartTrackingTouch(SeekBar seekBar) {
  106. }
  107.  
  108. @Override
  109. public void onStopTrackingTouch(SeekBar seekBar) {
  110. // seekbar的监听,滑动停止就修改系统媒体音量
  111. audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,seekBar.getProgress(),0);
  112. }
  113.  
  114. // 音量广播接收
  115. class VolumReceiver extends BroadcastReceiver{
  116. @Override
  117. public void onReceive(Context context, Intent intent) {
  118. seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
  119. }
  120. }
  121. }

Mainfest

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest package="com.fndroid.administrator.justforyou"
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4.  
  5. <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
  6. <uses-permission android:name="android.permission.WAKE_LOCK" />
  7. <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
  8.  
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@mipmap/ic_launcher"
  12. android:label="@string/app_name"
  13. android:supportsRtl="true"
  14. android:theme="@style/AppTheme">
  15. <activity android:name=".MainActivity">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN"/>
  18.  
  19. <category android:name="android.intent.category.LAUNCHER"/>
  20. </intent-filter>
  21. </activity>
  22. <service android:name=".NotificationService"
  23. android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
  24. <intent-filter>
  25. <action android:name="android.service.notification.NotificationListenerService" />
  26. </intent-filter>
  27. </service>
  28. </application>
  29.  
  30. </manifest>

gradle添加依赖,因为用了Snackbar

  1. dependencies {
  2. compile fileTree(dir: 'libs', include: ['*.jar'])
  3. testCompile 'junit:junit:4.12'
  4. compile 'com.android.support:appcompat-v7:23.1.1'
  5. compile 'com.android.support:design:23.1.1'
  6. }

Android开发学习之路-抢红包助手开发全攻略的更多相关文章

  1. Android 7.0终极开发者预览版全攻略!

    近日,Google的工程部副总裁Dave Burke在官方博客上正式发布开发者预览版5,此预览版是android 7.0 “牛轧糖”正式发布前最后一个预览版,同时也是在性能.功能上等多方面的表现上最接 ...

  2. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

  3. Android开发学习之路--基于vitamio的视频播放器(二)

      终于把该忙的事情都忙得差不多了,接下来又可以开始good good study,day day up了.在Android开发学习之路–基于vitamio的视频播放器(一)中,主要讲了播放器的界面的 ...

  4. Android开发学习之路--Android Studio cmake编译ffmpeg

      最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...

  5. Android开发学习之路--网络编程之xml、json

    一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载 ...

  6. Android开发学习之路--Activity之初体验

    环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...

  7. Android开发学习之路--Android系统架构初探

    环境搭建好了,最简单的app也运行过了,那么app到底是怎么运行在手机上的,手机又到底怎么能运行这些应用,一堆的电子元器件最后可以运行这么美妙的界面,在此还是需要好好研究研究.这里从芯片及硬件模块-& ...

  8. Android开发学习之路--MAC下Android Studio开发环境搭建

    自从毕业开始到现在还没有系统地学习android应用的开发,之前一直都是做些底层的驱动,以及linux上的c开发.虽然写过几个简单的app,也对android4.0.3的源代码做过部分的分析,也算入门 ...

  9. 2021年正确的Android逆向开发学习之路

    2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...

随机推荐

  1. Redis小结

    一. Redis简介 Redis是一个速度非常快的高性能的key-value存储系统.redis的出现,很大程度补偿了memcached这类key/value存储的不足.Redis支持存储五种valu ...

  2. 通过统计用户DNS解析记录,实现监控用户上网行为

    上次通过扫描抓包分析TTL的方式检测公司网络开放的端口,发现没有开放53端口(DNS),也就是在公司内部的主机只能用服务器自动分配的DNS,并且发现这是台内部服务器.今天发现bing上不去,检测后发现 ...

  3. NIO概述

    NIO API从Java1.4开始引用起就被广泛应用所使用.NIO API自带了IO非阻塞操作. java.nio.*包的结构: · Buffers 作为数据容器 · Chartsets 将容器中的数 ...

  4. java7笔记

    1 switch case 增加字符串类型,原理根据字符串hashcode以及string.equal比较2 数值字面量改进 新增二进制数值字面量 0b000004,在数值中间新增下划线方便阅读200 ...

  5. 基于Moodle的IT课程辅助教育平台搭建

    基于Moodle的IT课程辅助教育平台搭建 Moodle是一个开源课程管理系统(CMS),也被称为学习管理系统(LMS)或虚拟学习环境(VLE).它已成为深受世界各地教育工作者喜爱的一种为学生建立网上 ...

  6. java-IO

    框架图 IO(Input Output)流IO流用来处理设备之间的数据传输,Java对数据的操作是通过流的方式,用于操作流的对象都在IO包中. 流按操作数据分为两种:字节流与字符流 .流按流向分为:输 ...

  7. Java程序性能优化技巧

    Java程序性能优化技巧 多线程.集合.网络编程.内存优化.缓冲..spring.设计模式.软件工程.编程思想 1.生成对象时,合理分配空间和大小new ArrayList(100); 2.优化for ...

  8. 针对Excel的vbs操作

    工程中具有导入Excel数据的功能,但是需要满足一定的格式.为了在导入之前,对Excel的格式进行规范,对vbs脚本语言进行了一定的了解,并实现了一个可以将Excel全部单元格设为文本格式的vbs脚本 ...

  9. 书TO BE READED

    Books Created Wednesday 10 August 2011 1.<你的降落伞是什么颜色> 在求职书里面,我觉得这本书是最接近于圣经的. 第一版出现大概还是上世纪70年代末 ...

  10. MySql与Oracle的区别总结

    在平时工作中使用这两个数据库的时候要多一些,这两数据库的使用方面存在的一些各自不同的地方,许多面试官也会问这两个的区别.所以,凭着自己的一些经验个感触,来说说这二者的区别. 使用的群众:MySql中小 ...