类抢红包方案

主要引导用户授权、类似微信抢红包功能,也就是实时监听设备的通知栏消息,并可以捕获到通知的内容,然后进行对应的操作。之前尝试过很多方式,最后感觉前台service对于服务保活相对好使(据说这个微信也用过的方案),知情者可能要问了:前台service不是有个通知栏一直显示么?这样对用户来说不是很好。我们这里可以使用两个service互调来实现不显示通知栏,原理如下:

对于 API level < 18 :调用startForeground(ID, new Notification()),发送空的Notification ,图标则不会显示。
对于 API level >= 18:在需要提优先级的service A启动一个InnerService,两个服务同时startForeground,且绑定同样的 ID。Stop 掉InnerService ,这样通知栏图标即被移除。
前台服务类:

[html]  view plain  copy
  1. public class ForeService extends Service {  
  2.   
  3.     private final int FORESERVICE_PID = android.os.Process.myPid();  
  4.     private AssistServiceConnection mConnection;  
  5.   
  6.   
  7.     @Nullable  
  8.     @Override  
  9.     public IBinder onBind(Intent intent) {  
  10.         return null;  
  11.     }  
  12.   
  13.     @Override  
  14.     public void onCreate() {  
  15.         super.onCreate();  
  16.   
  17.         /**  
  18.          * 之前的额前台service(会显示通知栏)  
  19.          */  
  20. /*        //定义一个notification  
  21.         Notification.Builder builder1 = new Notification.Builder(this);  
  22.         builder1.setSmallIcon(R.mipmap.ic_launcher); //设置图标  
  23. //        builder1.setTicker("新消息");  
  24.         builder1.setContentTitle("My title"); //设置标题  
  25.         builder1.setContentText("My content"); //消息内容  
  26. //        builder1.setContentInfo("");//补充内容  
  27. //        builder1.setWhen(System.currentTimeMillis()); //发送时间  
  28. //        builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光  
  29. //        builder1.setAutoCancel(true);//打开程序后图标消失  
  30.         Intent intent = new Intent(this, MainActivity.class);  
  31.         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);  
  32.         builder1.setContentIntent(pendingIntent);  
  33.         Notification notification1 = builder1.build();  
  34.         //把该service创建为前台service  
  35.         startForeground(1, notification1);*/  
  36.   
  37.         setForeground();  
  38.     }  
  39.   
  40.   
  41.     @Override  
  42.     public int onStartCommand(Intent intent, int flags, int startId) {  
  43.         return super.onStartCommand(intent, flags, startId);  
  44.     }  
  45.   
  46.     private void setForeground() {  
  47.         //如果sdk<18 , 直接调用startForeground即可,不会在通知栏创建通知  
  48.         if (Build.VERSION.SDK_INT < 18) {  
  49.             this.startForeground(FORESERVICE_PID, getNotification());  
  50.             return;  
  51.         }  
  52.   
  53.         if (null == mConnection) {  
  54.             mConnection = new AssistServiceConnection();  
  55.         }  
  56.   
  57.         this.bindService(new Intent(this, AssistService.class), mConnection,  
  58.                 Service.BIND_AUTO_CREATE);  
  59.     }  
  60.   
  61.     public Notification getNotification() {  
  62.         //定义一个notification  
  63.         Notification.Builder builder1 = new Notification.Builder(this);  
  64.         builder1.setSmallIcon(R.mipmap.ic_launcher); //设置图标  
  65. //        builder1.setTicker("新消息");  
  66.         builder1.setContentTitle("My title"); //设置标题  
  67.         builder1.setContentText("My content"); //消息内容  
  68. //        builder1.setContentInfo("");//补充内容  
  69. //        builder1.setWhen(System.currentTimeMillis()); //发送时间  
  70. //        builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光  
  71. //        builder1.setAutoCancel(true);//打开程序后图标消失  
  72.         Intent intent = new Intent(this, MainActivity.class);  
  73.         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);  
  74.         builder1.setContentIntent(pendingIntent);  
  75.         Notification notification1 = builder1.build();  
  76.         return notification1;  
  77.     }  
  78.   
  79.   
  80.     private class AssistServiceConnection implements ServiceConnection {  
  81.   
  82.         @Override  
  83.         public void onServiceConnected(ComponentName name, IBinder binder) {  
  84.             // sdk >=18 的,会在通知栏显示service正在运行,这里不要让用户感知,所以这里的实现方式是利用2个同进程的service,利用相同的notificationID,  
  85.             // 2个service分别startForeground,然后只在1个service里stopForeground,这样即可去掉通知栏的显示  
  86.             Service assistService = ((AssistService.LocalBinder) binder)  
  87.                     .getService();  
  88.             ForeService.this.startForeground(FORESERVICE_PID, getNotification());  
  89.             assistService.startForeground(FORESERVICE_PID, getNotification());  
  90.             assistService.stopForeground(true);  
  91.   
  92.             ForeService.this.unbindService(mConnection);  
  93.             mConnection = null;  
  94.         }  
  95.   
  96.         @Override  
  97.         public void onServiceDisconnected(ComponentName name) {  
  98.   
  99.         }  
  100.     }  
  101. }  
辅佐服务类:

[html]  view plain  copy
  1. public class AssistService extends Service {  
  2.   
  3.     public class LocalBinder extends Binder {  
  4.         public AssistService getService() {  
  5.             return AssistService.this;  
  6.         }  
  7.     }  
  8.   
  9.     @Nullable  
  10.     @Override  
  11.     public IBinder onBind(Intent intent) {  
  12.         return new LocalBinder();  
  13.     }  
  14. }  
然后开启前台service即可。


下面给介绍一种新的保活方案:NotificationListenerService。NotificationListenerService就是一个监听通知的服务,只要手机收到了通知,NotificationListenerService都能监听到,即时用户把进程杀死,也能重启。

NotificationListenerService 的使用范围也挺广的,比如我们熟知的抢红包,智能手表同步通知,通知栏去广告工具等,都是利用它来完成的。所以,我也想赶时髦地好好利用这把“利器”。最后方案也就出来了:在 Android 4.3 以下(API < 18)使用 AccessibilityService 来读取新通知,在 Android 4.3 及以上(API >= 18)使用 NotificationListenerService 来满足需求。

NotificationListenerService

在这里,我们就做一个小需求:实时检测微信的新通知,如果该通知是微信红包的话,就进入微信聊天页面。

首先创建一个 WeChatNotificationListenerService 继承 NotificationListenerService 。然后在 AndroidManifest.xml 中进行声明相关权限和 <intent-filter> :

1
2
3
4
5
6
7
<service android:name="com.yuqirong.listenwechatnotification.WeChatNotificationListenerService"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">

<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

然后一般会重写下面这三个方法:

  • onNotificationPosted(StatusBarNotification sbn) :当有新通知到来时会回调;
  • onNotificationRemoved(StatusBarNotification sbn) :当有通知移除时会回调;
  • onListenerConnected() :当 NotificationListenerService 是可用的并且和通知管理器连接成功时回调。

onNotificationPosted(StatusBarNotification sbn)

下面我们来看看 NotificationListenerService 中的重点: onNotificationPosted(StatusBarNotification sbn) 方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// 如果该通知的包名不是微信,那么 pass 掉
if (!"com.tencent.mm".equals(sbn.getPackageName())) {
return;
}
Notification notification = sbn.getNotification();
if (notification == null) {
return;
}
PendingIntent pendingIntent = null;
// 当 API > 18 时,使用 extras 获取通知的详细信息
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Bundle extras = notification.extras;
if (extras != null) {
// 获取通知标题
String title = extras.getString(Notification.EXTRA_TITLE, "");
// 获取通知内容
String content = extras.getString(Notification.EXTRA_TEXT, "");
if (!TextUtils.isEmpty(content) && content.contains("[微信红包]")) {
pendingIntent = notification.contentIntent;
}
}
} else {
// 当 API = 18 时,利用反射获取内容字段
List<String> textList = getText(notification);
if (textList != null && textList.size() > 0) {
for (String text : textList) {
if (!TextUtils.isEmpty(text) && text.contains("[微信红包]")) {
pendingIntent = notification.contentIntent;
break;
}
}
}
}
// 发送 pendingIntent 以此打开微信
try {
if (pendingIntent != null) {
pendingIntent.send();
}
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}

从上面的代码可知,对于分析 Notification 的内容分为了两种:

  • 当 API > 18 时,利用 Notification.extras 来获取通知内容。extras 是在 API 19 时被加入的;
  • 当 API = 18 时,利用反射获取 Notification 中的内容。具体的代码在下方。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public List<String> getText(Notification notification) {
if (null == notification) {
return null;
}
RemoteViews views = notification.bigContentView;
if (views == null) {
views = notification.contentView;
}
if (views == null) {
return null;
}
// Use reflection to examine the m_actions member of the given RemoteViews object.
// It's not pretty, but it works.
List<String> text = new ArrayList<>();
try {
Field field = views.getClass().getDeclaredField("mActions");
field.setAccessible(true);
@SuppressWarnings("unchecked")
ArrayList<Parcelable> actions = (ArrayList<Parcelable>) field.get(views);
// Find the setText() and setTime() reflection actions
for (Parcelable p : actions) {
Parcel parcel = Parcel.obtain();
p.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
// The tag tells which type of action it is (2 is ReflectionAction, from the source)
int tag = parcel.readInt();
if (tag != 2) continue;
// View ID
parcel.readInt();
String methodName = parcel.readString();
if (null == methodName) {
continue;
} else if (methodName.equals("setText")) {
// Parameter type (10 = Character Sequence)
parcel.readInt();
// Store the actual string
String t = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel).toString().trim();
text.add(t);
}
parcel.recycle();
}
} catch (Exception e) {
e.printStackTrace();
}
return text;
}

凭着 onNotificationPosted(StatusBarNotification sbn) 方法就已经可以完成监听微信通知并打开的动作了。下面我们来看一下其他关于 NotificationListenerService 的二三事。

取消通知

有了监听,NotificationListenerService 自然提供了可以取消通知的方法。取消通知的方法有:

  • cancelNotification(String key) :是 API >= 21 才可以使用的。利用 StatusBarNotification 的 getKey() 方法来获取 key 并取消通知。
  • cancelNotification(String pkg, String tag, int id) :在 API < 21 时可以使用,在 API >= 21 时使用此方法来取消通知将无效,被废弃。

最后,取消通知的方法:

1
2
3
4
5
6
7
public void cancelNotification(StatusBarNotification sbn) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cancelNotification(sbn.getKey());
} else {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
}

检测通知监听服务是否被授权

1
2
3
4
5
6
7
public boolean isNotificationListenerEnabled(Context context) {
Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages(this);
if (packageNames.contains(context.getPackageName())) {
return true;
}
return false;
}

打开通知监听设置页面

1
2
3
4
5
6
7
8
9
10
11
12
13
public void openNotificationListenSettings() {
try {
Intent intent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
} else {
intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
}
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}

被杀后再次启动时,监听不生效的问题

这个问题来源于知乎问题: NotificationListenerService不能监听到通知,研究了一天不知道是什么原因?

从问题的回答中可以了解到,是因为 NotificationListenerService 被杀后再次启动时,并没有去 bindService ,所以导致监听效果无效。

最后,在回答中还给出了解决方案:利用 NotificationListenerService 先 disable 再 enable ,重新触发系统的 rebind 操作。代码如下:

1
2
3
4
5
6
7
private void toggleNotificationListenerService() {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, com.fanwei.alipaynotification.ui.AlipayNotificationListenerService.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(this, com.fanwei.alipaynotification.ui.AlipayNotificationListenerService.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

该方法使用前提是 NotificationListenerService 已经被用户授予了权限,否则无效。另外,在自己的小米手机上实测,重新完成 rebind 操作需要等待 10 多秒(我的手机测试过大概在 13 秒左右)。幸运的是,官方也已经发现了这个问题,在 API 24 中提供了 requestRebind(ComponentName componentName) 方法来支持重新绑定。

AccessibilityService

讲完了 NotificationListenerService 之后,按照前面说的那样,在 API < 18 的时候使用 AccessibilityService 。

同样,创建一个 WeChatAccessibilityService ,并且在 AndroidManifest.xml 中进行声明:

1
2
3
4
5
6
7
8
9
10
11
<service
android:name="com.yuqirong.listenwechatnotification.WeChatAccessibilityService"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessible_service_config" />
</service>

声明之后,还要对 WeChatAccessibilityService 进行配置。需要在 res 目录下新建一个 xml 文件夹,在里面新建一个 accessible_service_config.xml 文件:

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagIncludeNotImportantViews"
android:canRetrieveWindowContent="true"
android:description="@string/app_name"
android:notificationTimeout="100"
android:packageNames="com.tencent.mm" />

最后就是代码了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class WeChatAccessibilityService extends AccessibilityService {

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (Build.VERSION.SDK_INT < 18) {
Notification notification = (Notification) event.getParcelableData();
List<String> textList = getText(notification);
if (textList != null && textList.size() > 0) {
for (String text : textList) {
if (!TextUtils.isEmpty(text) &&
text.contains("[微信红包]")) {
final PendingIntent pendingIntent = notification.contentIntent;
try {
if (pendingIntent != null) {
pendingIntent.send();
}
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
break;
}
}
}
}

@Override
public void onInterrupt() {

}

}

看了一圈 WeChatAccessibilityService 的代码,发现和 WeChatNotificationListenerService 在 API < 18 时处理的逻辑是一样的,getText(notification) 方法就是上面那个,在这里就不复制粘贴了,基本没什么好讲的了。

有了 WeChatAccessibilityService 之后,在 API < 18 的情况下也能监听通知啦。\(^ο^)/

我们终于实现了当初许下的那个需求了。 cry …

总结

除了监听通知之外,AccessibilityService 还可以进行模拟点击、检测界面变化等功能。具体的可以在 GitHub 上搜索抢红包有关的 Repo 进行深入学习。

而 NotificationListenerService 的监听通知功能更加强大,也更加专业。在一些设备上,如果 NotificationListenerService 被授予了权限,那么可以做到该监听进程不死的效果,也算是另类的进程保活。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值