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() {

super.onCreate();

}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)

@Override

public void onNotificationPosted(StatusBarNotification sbn) {

// Toast.makeText(this,"收到消息",Toast.LENGTH_SHORT).show();

Notification notification = sbn.getNotification();

if (notification == null) {

return;

}

Bundle extras = notification.extras;

if (extras != null) {

//包名

String pkg = sbn.getPackageName();

// 获取通知标题

String title = extras.getString(Notification.EXTRA_TITLE, "");

// 获取通知内容

String content = extras.getString(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) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

if (!isNotificationListenerEnabled(this)){

openNotificationListenSettings();

}

toggleNotificationListenerService();

// startService(new Intent(this,PayReceiver.class));

}

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

public boolean isNotificationListenerEnabled(Context context) {

Set packageNames = NotificationManagerCompat.getEnabledListenerPackages(this);

if (packageNames.contains(context.getPackageName())) {

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();

}

}

//把应用的Notif

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值