본문 바로가기

안드로이드

안드로이드 문자 , 카카오톡 알림 캐치

반응형

 

1.메니페스트에 서비스 선언

<service android:name=".NotificationListener"    android:exported="true"

    android:label="My Notification Listener"

    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">

    <intent-filter>

        <action android:name="android.service.notification.NotificationListenerService" />

    </intent-filter>

</service>

 

 

2. 메인에서 시스템 권한 요청

if (!permissionGrantred()) {

    Intent gt= new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");

    startActivity(gt);

}

 

 

private boolean permissionGrantred() {

    Set<String> sets = NotificationManagerCompat.getEnabledListenerPackages(this);

    if (sets != null && sets.contains(getPackageName())) {

        return true;

    } else {

        return false;

    }

}

 

 

3.서비스 구현

 

  1. com.android.messaging: 이 패키지명은 Google의 Android Messages 앱에 해당합니다. 이 앱은 일부 Android 디바이스에서 기본 문자 메시징 앱으로 제공됩니다.
  2. com.samsung.android.messaging: 삼성 디바이스에서는 Samsung Messages 앱이 기본 문자 메시징 앱으로 제공되며, 패키지명은 이와 같습니다.
  3. com.lge.message: LG 디바이스에서는 LG 메시지 앱이 기본 문자 메시징 앱으로 사용되며, 패키지명은 위와 같습니다.
  4. com.htc.sense.messaging: HTC 디바이스에서는 HTC Sense의 일부로 제공되는 문자 메시징 앱의 패키지명입니다.
  5. com.motorola.messaging: 몇몇 모토로라 디바이스에서는 Motorola의 문자 메시징 앱이 사용되며, 패키지명은 위와 같습니다.

 

public class NotificationListener extends NotificationListenerService {

 

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {

        super.onNotificationRemoved(sbn);

 

        Log.d(TAG, "onNotificationRemoved ~ " +

                " packageName: " + sbn.getPackageName() +

                " id: " + sbn.getId());

    }

 

    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {

        super.onNotificationPosted(sbn);

 

        Bundle extras = sbn.getNotification().extras;

 

        CharSequence title = "";

 

        if (extras.getCharSequence(Notification.EXTRA_TITLE) == null) {

            title = "";

        } else {

            title = extras.getCharSequence(Notification.EXTRA_TITLE);

        }

 

        CharSequence text = "";

 

        if (extras.getCharSequence(Notification.EXTRA_TEXT) == null) {

            text = "";

        } else {

            text = extras.getCharSequence(Notification.EXTRA_TEXT);

        }

 

 

        //KT일때  문자(com.samsung.android.messaging)  , LG일때 (com.android.cellbroadcastreceiver)

if (sbn.getPackageName().equals("com.samsung.android.messaging") || sbn.getPackageName().equals("com.android.cellbroadcastreceiver")

        || sbn.getPackageName().equals("com.android.messaging") || sbn.getPackageName().equals("com.lge.message")

        || sbn.getPackageName().equals("com.htc.sense.messaging") || sbn.getPackageName().equals("com.motorola.messaging")) {

 

                Log.d("myLog", "pac name  " + sbn.getPackageName());

                Log.d("myLog", "title " + title);

                Log.d("myLog", "text " + text);

 

        }else{

                  if (sbn.getPackageName().equals("com.kakao.talk")){ //카카오톡일때

                  }

 

       }

 

 

 

 

    }

반응형