|
18 | 18 | import android.support.annotation.NonNull; |
19 | 19 | import android.support.v4.app.ActivityCompat; |
20 | 20 | import android.support.v4.app.NotificationCompat; |
| 21 | +import android.support.v4.app.NotificationManagerCompat; |
21 | 22 | import android.support.v7.app.AppCompatActivity; |
22 | 23 | import android.text.TextUtils; |
23 | 24 | import android.util.Log; |
|
35 | 36 | import java.security.MessageDigest; |
36 | 37 | import java.security.NoSuchAlgorithmException; |
37 | 38 | import java.util.Date; |
| 39 | +import java.util.Set; |
38 | 40 |
|
39 | 41 | import okhttp3.Call; |
40 | 42 | import okhttp3.Callback; |
@@ -79,6 +81,16 @@ protected void onCreate(Bundle savedInstanceState) { |
79 | 81 | btnStart.setText("检测服务状态"); |
80 | 82 | } |
81 | 83 |
|
| 84 | + if (!isNotificationListenersEnabled()) { |
| 85 | + gotoNotificationAccessSetting(); |
| 86 | + } |
| 87 | + |
| 88 | +// if (!isNotificationListenerServiceEnabled(this)){ |
| 89 | +// |
| 90 | +// } |
| 91 | + toggleNotificationListenerService(this); |
| 92 | + |
| 93 | + |
82 | 94 |
|
83 | 95 | //读入保存的配置数据并显示 |
84 | 96 | SharedPreferences read = getSharedPreferences("vone", MODE_PRIVATE); |
@@ -310,10 +322,12 @@ private boolean gotoNotificationAccessSetting(Context context) { |
310 | 322 |
|
311 | 323 |
|
312 | 324 | public void checkPush(View v){ |
313 | | - if (!btnStart.getText().equals("检测服务状态")){ |
314 | | - Toast.makeText(MainActivity.this, "请先开启服务!", Toast.LENGTH_SHORT).show(); |
315 | | - return; |
316 | | - } |
| 325 | +// if (!btnStart.getText().equals("检测服务状态")){ |
| 326 | +// Toast.makeText(MainActivity.this, "请先开启服务!", Toast.LENGTH_SHORT).show(); |
| 327 | +// return; |
| 328 | +// } |
| 329 | + |
| 330 | + |
317 | 331 |
|
318 | 332 | Notification mNotification; |
319 | 333 | NotificationManager mNotificationManager; |
@@ -344,7 +358,24 @@ public void checkPush(View v){ |
344 | 358 | } |
345 | 359 |
|
346 | 360 |
|
| 361 | + private static boolean isNotificationListenerServiceEnabled(Context context) { |
| 362 | + Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages(context); |
| 363 | + if (packageNames.contains(context.getPackageName())) { |
| 364 | + return true; |
| 365 | + } |
| 366 | + return false; |
| 367 | + } |
| 368 | + |
| 369 | + private void toggleNotificationListenerService(Context context) { |
| 370 | + PackageManager pm = context.getPackageManager(); |
| 371 | + pm.setComponentEnabledSetting(new ComponentName(context, NeNotificationService2.class), |
| 372 | + PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
347 | 373 |
|
| 374 | + pm.setComponentEnabledSetting(new ComponentName(context, NeNotificationService2.class), |
| 375 | + PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); |
| 376 | + |
| 377 | + Toast.makeText(MainActivity.this, "监听服务启动中...", Toast.LENGTH_SHORT).show(); |
| 378 | + } |
348 | 379 |
|
349 | 380 |
|
350 | 381 |
|
@@ -372,6 +403,46 @@ public static String md5(String string) { |
372 | 403 | return ""; |
373 | 404 | } |
374 | 405 |
|
| 406 | + public boolean isNotificationListenersEnabled() { |
| 407 | + String pkgName = getPackageName(); |
| 408 | + final String flat = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners"); |
| 409 | + if (!TextUtils.isEmpty(flat)) { |
| 410 | + final String[] names = flat.split(":"); |
| 411 | + for (int i = 0; i < names.length; i++) { |
| 412 | + final ComponentName cn = ComponentName.unflattenFromString(names[i]); |
| 413 | + if (cn != null) { |
| 414 | + if (TextUtils.equals(pkgName, cn.getPackageName())) { |
| 415 | + return true; |
| 416 | + } |
| 417 | + } |
| 418 | + } |
| 419 | + } |
| 420 | + return false; |
| 421 | + } |
| 422 | + protected boolean gotoNotificationAccessSetting() { |
| 423 | + try { |
| 424 | + Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); |
| 425 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 426 | + startActivity(intent); |
| 427 | + return true; |
| 428 | + |
| 429 | + } catch (ActivityNotFoundException e) {//普通情况下找不到的时候需要再特殊处理找一次 |
| 430 | + try { |
| 431 | + Intent intent = new Intent(); |
| 432 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 433 | + ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.Settings$NotificationAccessSettingsActivity"); |
| 434 | + intent.setComponent(cn); |
| 435 | + intent.putExtra(":settings:show_fragment", "NotificationAccessSettings"); |
| 436 | + startActivity(intent); |
| 437 | + return true; |
| 438 | + } catch (Exception e1) { |
| 439 | + e1.printStackTrace(); |
| 440 | + } |
| 441 | + Toast.makeText(this, "对不起,您的手机暂不支持", Toast.LENGTH_SHORT).show(); |
| 442 | + e.printStackTrace(); |
| 443 | + return false; |
| 444 | + } |
| 445 | + } |
375 | 446 |
|
376 | 447 |
|
377 | 448 | @Override |
|
0 commit comments