我不知道为什么NotificationListenerService对我不起作用,看起来很简单但对我不起作用。我发现很多都没有解决我的问题。我认为我的服务已被杀死。所以我使用用于检查通知的adb
adb shell dempsys notification
它告诉我我的服务不是活的,但我不知道为什么我的服务被杀
Notification listeners:
All notification listeners (2) enabled for current profiles:
ComponentInfo{com.example.user.myservicedemo/com.example.user.myservicedemo.MyListernService}
ComponentInfo{com.meizu.battery/com.meizu.power.apps.appnotification.PermanentNotificationService}
Live notification listeners (5):
ComponentInfo{com.android.systemui/com.android.systemui.statusbar.phone.PhoneStatusBar} (user -1): android.service.notification.INotification
Listener$Stub$Proxy@37943ab SYSTEM
ComponentInfo{android.ext.services/android.ext.services.notification.Ranker} (user 0): android.service.notification.INotificationListener$Stu
b$Proxy@e821a08 SYSTEM GUEST
ComponentInfo{com.flyme.systemuitools/com.flyme.systemuitools.SystemuitoolsApplication} (user 0): android.service.notification.INotificationL
istener$Stub$Proxy@b83b5a1 SYSTEM
ComponentInfo{com.meizu.battery/com.meizu.power.apps.appnotification.PermanentNotificationService} (user 0): android.service.notification.INo
tificationListener$Stub$Proxy@3f193c6
ComponentInfo{com.flyme.systemuitools/com.flyme.systemuitools.aod.model.AODModelImpl} (user 0): android.service.notification.INotificationLis
tener$Stub$Proxy@a066f87 SYSTEM
MyListernService
public class MyListernService extends NotificationListenerService {
private boolean mStartCompatibility ;
public static MyListernService instance;
@Override
public void onCreate() {
super.onCreate();
Log.e("test","onCreate");
instance = this;
mStartCompatibility = getApplicationInfo().targetSdkVersion
< Build.VERSION_CODES.ECLAIR;
if (startService(new Intent(this, KeepLiveSevice.class)) == null)
throw new RuntimeException("Couldn't find " + KeepLiveSevice.class.getSimpleName());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("test","onStartCommand");
toggleNotificationListenerService();
return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.e("test","onNotificationPosted");
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Log.e("test","onNotificationRemoved");
}
@Override
public void onListenerConnected() {
super.onListenerConnected();
Log.e("test","onListenerConnected");
}
@Override
public void onListenerDisconnected() {
super.onListenerDisconnected();
Log.e("test","onListenerDisconnected");
}
@Override
public void onDestroy() {
super.onDestroy();
instance = null;
Log.e("test","onDestroy");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void toggleNotificationListenerService() {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, com.example.user.myservicedemo.MyListernService.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(this,com.example.user.myservicedemo.MyListernService.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
}
和MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!isNotificationListenerEnabled(this)){
openNotificationListenSettings();
}
startService(new Intent(this,MyListernService.class));
}
public boolean isNotificationListenerEnabled(Context context) {
Set<String> 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();
}
}
keepLiveService
public class KeepLiveSevice extends Service {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (MyListernService.instance == null)
throw new RuntimeException(MyListernService.class.getSimpleName() + " not running");
//Set both services to foreground using the same notification id, resulting in just one notification
startForeground(MyListernService.instance);
startForeground(this);
//Cancel this service's notification, resulting in zero notifications
stopForeground(true);
//Stop this service so we don't waste RAM.
//Must only be called *after* doing the work or the notification won't be hidden.
stopSelf();
return START_NOT_STICKY;
}
private static final int NOTIFICATION_ID = 10;
private static void startForeground(Service service) {
Notification notification = new Notification.Builder(service).getNotification();
service.startForeground(NOTIFICATION_ID, notification);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
的AndroidManifest.xml
<service android:name=".MyListernService"
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>
<service android:name=".KeepLiveSevice"/>
答案 0 :(得分:0)
您必须授予对应用通知的访问权限。
基本上转到手机的设置&gt;然后,通知会授权您的应用访问通知