我是超级初学者。现在,我正在尝试使用NotificationListenerService进行安卓通知。
问题:
应用程序不发布通知,但我可以在logcat中检查(A)应用程序调试日志。
当发布通知时,我想自动将通知作为Bapplication发布。
(我在做Bapplication。)
现在:
我可以得到一个应用程序包的名称,当它发布。然后是...stop,我如何从Bapplication发布通知(id和包名)?用广播接收机?
NotificationService.java
package com.testnotification;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
public class NotificationService extends NotificationListenerService {
private String TAG = "Notification";
private String Sample = "com.notification.sample"; ///this is [A]application
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String packagename = sbn.getPackageName();
if(packagename.equals(Sample)){
showLog(sbn);
}
}
private void showLog( StatusBarNotification sbn ){
int id = sbn.getId();
String name = sbn.getPackageName();
Log.d(TAG,"id:" + id +
" name:" + name);
}
}AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testnotification">
<application
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/Theme.Testnotification">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".NotificationService"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:exported="true">
<intent-filter>
<action android:name=
"android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>
</manifest>MainActivity.java(空的)
请教我。
发布于 2022-02-25 09:37:57
您是否检查用户是否授予了通知侦听器的应用程序权限?
/**
* Is Notification Service Enabled.
* Verifies if the notification listener service is enabled.
* Got it from: https://github.com/kpbird/NotificationListenerService-Example/blob/master/NLSExample/src/main/java/com/kpbird/nlsexample/NLService.java
* @return True if enabled, false otherwise.
*/
private boolean isNotificationServiceEnabled(){
String pkgName = getPackageName();
final String flat = Settings.Secure.getString(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;
}如果没有权限,可以为用户打开设置应用程序以启用权限。
/**
* Build Notification Listener Alert Dialog.
* Builds the alert dialog that pops up if the user has not turned
* the Notification Listener Service on yet.
* @return An alert dialog which leads to the notification enabling screen
*/
private AlertDialog buildNotificationServiceAlertDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(R.string.notification_listener_service);
alertDialogBuilder.setMessage(R.string.notification_listener_service_explanation);
alertDialogBuilder.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));
}
});
alertDialogBuilder.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// If you choose to not enable the notification listener
// the app. will not work as expected
}
});
return(alertDialogBuilder.create());
}下面是我在GitHub中找到的通知侦听器的完整项目
https://github.com/Chagall/notification-listener-service-example
https://stackoverflow.com/questions/71263046
复制相似问题