Bin*_*ozo 4 java android android-studio
我是 Android 新手,我正在尝试开发一个应用程序,它可以读取 Whatsapp 通知并对其执行某些操作:)
我尝试了不同的方法来检查用户是否授予了“BIND_NOTIFICATION_LISTENER_SERVICE”权限,但没有任何效果。它总是说,未授予许可。但事实并非如此。这是代码:
if(ContextCompat.checkSelfPermission(this, Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE) == PackageManager.PERMISSION_GRANTED){
Log.i(TAG, "App has permission!");
} else
Log.i(TAG, "App hasn't permission " + ContextCompat.checkSelfPermission(this, Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE));
Run Code Online (Sandbox Code Playgroud)
这是我在应用程序中更改的设置:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
显现:
android:label="Whatsapp Nachrichten leser"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
侦听器服务完美运行。它只是说,我没有权限。(对不起,我的英语不好)
这是解决方案:
private boolean isNotificationServiceEnabled(Context c){
String pkgName = c.getPackageName();
final String flat = Settings.Secure.getString(c.getContentResolver(),
"enabled_notification_listeners");
if (!TextUtils.isEmpty(flat)) {
final String[] names = flat.split(":");
for (int i = 0; i < names.length; i++) {
final ComponentName cn = ComponentName.unflattenFromString(names[i]);
if (cn != null) {
if (TextUtils.equals(pkgName, cn.getPackageName())) {
return true;
}
}
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1564 次 |
| 最近记录: |