Android监听⽀付宝收款信息,#监听通知栏获取⽀付宝到账信
息
监听通知栏获取⽀付宝到账信息
[toc]
定义service
@SuppressLint("OverrideAbstract")
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class PayReceiver extends NotificationListenerService {
private static final String TAG = "lht";
@Override
public void onCreate() {
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Toast.makeText(this,"收到消息",Toast.LENGTH_SHORT).show();
Notification notification = Notification();
if (notification == null) {
return;
}
Bundle extras = as;
if (extras != null) {
//包名
String pkg = PackageName();
// 获取通知标题
String title = String(Notification.EXTRA_TITLE, "");
// 获取通知内容
String content = String(Notification.EXTRA_TEXT, "");
Log.i(TAG, String.format("收到通知,包名:%s,标题:%s,内容:%s", pkg, title, content));
//处理
processOnReceive(pkg, title, content);
}
权限获取
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
if (!isNotificationListenerEnabled(this)){
openNotificationListenSettings();
}
toggleNotificationListenerService();
// startService(new Intent(this,PayReceiver.class));
}
//检测通知监听服务是否被授权
public boolean isNotificationListenerEnabled(Context context) {
Set packageNames = EnabledListenerPackages(this);
if (PackageName())) {
return true;
}
return false;
}
//打开通知监听设置页⾯
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实现类disable再enable,即可触发系统rebind操作
private void toggleNotificationListenerService() {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(this, PayReceiver.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); pm.setComponentEnabledSetting(
new ComponentName(this, PayReceiver.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); }
}
android:name=".ReceiverApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:name=".PayReceiver"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
数据整理
/**
* 解析内容字符串,提取⾦额
*
* @param content
* @return
*/
private static String parseMoney(String content) {
Pattern pattern = Patternpile("付款(([1-9]\\d*)|0)(\\.(\\d){0,2})?元");
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
String tmp = up();
Pattern patternnum = Patternpile("(([1-9]\\d*)|0)(\\.(\\d){0,2})?");
Matcher matchernum = patternnum.matcher(tmp);
if (matchernum.find())
up();
}
return null;
}
/**
* 验证消息的合法性,防⽌⾮官⽅消息被处理
*
* @param title
* @param content
* @param gateway
* @return
*/
private static boolean checkMsgValid(String title, String content, String gateway) { if ("wxpay".equals(gateway)) {
//⽀付的消息格式
//1条:标题:⽀付,内容:⽀付收款0.01元(朋友到店)
//多条:标题:⽀付,内容:[4条]⽀付: ⽀付收款1.01元(朋友到店)
Pattern pattern = Patternpile("^((\\[\\+?\\d+条])?⽀付:|⽀付收款)"); Matcher matcher = pattern.matcher(content);
return "⽀付".equals(title) && matcher.find();
} else if ("alipay".equals(gateway)) {
//⽀付宝的消息格式,标题:⽀付宝通知,内容:⽀付宝成功收款1.00元。
return "收钱码".equals(title);
}
return false;
}
/**
* 提取字符串中的数字
* @param strInput
* @return
*/
public static String getNum(String strInput) {
//匹配指定范围内的数字
String regEx = "[^0-9]";
//Pattern是⼀个正则表达式经编译后的表现模式
Pattern p = Patternpile(regEx);
// ⼀个Matcher对象是⼀个状态机器,它依据Pattern对象做为匹配模式对字符串展开匹配检查。Matcher m = p.matcher(strInput);
//将输⼊的字符串中⾮数字部分⽤空格取代并存⼊⼀个字符串
String string = m.replaceAll(" ").trim();
//以空格为分割符在讲数字存⼊⼀个字符串数组中
String[] strArr = string.split(" ");
StringBuffer stringBuffer = new StringBuffer();
//遍历数组转换数据类型输出
for (String s : strArr) {
stringBuffer.append(s);
System.out.println(Integer.parseInt(s));
}
String num = String();
System.out.println("num is " + num);
return num;
}
完整代码
package com.ly.paycallback;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.os.Build;
android获取真正的签名import android.os.Bundle;
import ification.NotificationListenerService;
import ification.StatusBarNotification;
TextUtils;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import Matcher;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论