mContext1 = getApplicationContext();//连接手表通知
sp = mContext1.getSharedPreferences(GlobalVariable.SettingSP, 0);
mWriteCommand = WriteCommandToBLE.getInstance(mContext1);
mainactivity.java
package com.example.cieshi;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!isNotificationListenerEnabled(this)){
openNotificationListenSettings();
}
toggleNotificationListenerService();
}
//检测通知监听服务是否被授权
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();
}
}
//把应用的NotificationListenerService实现类disable再enable,即可触发系统rebind操作
private void toggleNotificationListenerService() {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(this, NotificationCollectorService.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(
new ComponentName(this, NotificationCollectorService.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
}
package com.example.cieshi;
import android.app.Notification;
import android.os.Build;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import androidx.annotation.RequiresApi;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationCollectorService extends NotificationListenerService {
//来通知时的调用
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Notification notification = sbn.getNotification();
if (notification == null) {
return;
}
Bundle extras = notification.extras;
String content = "";
if (extras != null) {
// 获取通知标题
String title = extras.getString(Notification.EXTRA_TITLE, "");
// 获取通知内容
content = extras.getString(Notification.EXTRA_TEXT, "");
Log.i("包名:",sbn.getPackageName()+"标题:"+title+"内容:"+content);
}
switch (sbn.getPackageName()){
case "com.tencent.mm":
Log.i("微信",content);
break;
case "com.android.mms":
Log.i("短信信",content);
break;
case "com.tencent.mqq":
Log.i("qq",content);
break;
case "com.tencent.tim":
Log.i("tim",content);
break;
case "com.android.incallui":
Log.i("电话",content);
break;
}
}
//删除通知时的调用
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Notification notification = sbn.getNotification();
if (notification == null) {
return;
}
Bundle extras = notification.extras;
String content = "";
if (extras != null) {
// 获取通知标题
String title = extras.getString(Notification.EXTRA_TITLE, "");
// 获取通知内容
content = extras.getString(Notification.EXTRA_TEXT, "");
Log.i("删包名:",sbn.getPackageName()+"标题:"+title+"内容:"+content);
}
switch (sbn.getPackageName()){
case "com.tencent.mm":
Log.i("删微信",content);
break;
case "com.android.mms":
Log.i("删短信",content);
break;
case "com.tencent.mqq":
Log.i("删qq",content);
break;
case "com.tencent.tim":
Log.i("删tim",content);
break;
case "com.android.incallui":
Log.i("删电话",content);
break;
}
}
}
<service android:name=".NotificationCollectorService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>