2017-01-07 10 views
0

通知バーから受信したWhatsAppメッセージを読み取ろうとしています。通知からWhatsAppメッセージを読み取る

のWhatsAppメッセージを受信した場合、私は、そうすることを

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
public class MyAccessibilityService extends AccessibilityService { 

    private final AccessibilityServiceInfo info = new AccessibilityServiceInfo(); 
    private static final String TAG = "MyAccessibilityService"; 

    public static TextView title; 
    public static TextView inbox; 
    public static TextView text; 


    @Override 
    protected void onServiceConnected() 
    { 
     Log.d("onServiceConnected", "ServiceConnected"); 
     try 
     { 
      AccessibilityServiceInfo info = new AccessibilityServiceInfo(); 

      info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED; 
      info.packageNames = new String[] 
      {"com.whatsapp"}; 


      info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK; 

      info.notificationTimeout = 100; 

      setServiceInfo(info); 
     } 
     catch(Exception e) 
     { 
      Log.d("ERRORonServiceConnected", e.toString()); 
     } 
    } 



    @Override 
    public void onAccessibilityEvent(AccessibilityEvent event) 
    { 
     try 
     { 
      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      Parcelable data = event.getParcelableData(); 

      if(data != null) 
      { 
       Notification notification = (Notification) data; 

       RemoteViews remoteView = notification.contentView; 

       ViewGroup localView = (ViewGroup) inflater.inflate(remoteView.getLayoutId(), null); 

       remoteView.reapply(getApplicationContext(), localView); 

       Resources resources = null; 

       PackageManager pkm = getPackageManager(); 

       try 
       { 
        resources = pkm.getResourcesForApplication("com.whatsapp"); 
       } 
       catch (PackageManager.NameNotFoundException e) 
       { 
        e.printStackTrace(); 
       } 

       if (resources == null) 
        return; 

       int TITLE = resources.getIdentifier("android:id/title", null, null); 

       int INBOX = resources.getIdentifier("android:id/big_text", null, null); 

       int TEXT = resources.getIdentifier("android:id/text", null, null); 

       String packagename = String.valueOf(event.getPackageName()); 

       title = (TextView) localView.findViewById(TITLE); 

       inbox = (TextView) localView.findViewById(INBOX); 

       text = (TextView) localView.findViewById(TEXT); 

       Log.d("NOTIFICATION Package : ", packagename); 

       Log.d("NOTIFICATION Title : ", title.getText().toString()); 

       Log.d("got x messages : ", text.getText().toString()); 

       Log.d("NOTIFICATION inbox : ", inbox.getText().toString()); 
     } 
     } 
     catch(Exception e) 
     { 
      Log.e("onAccessibilityEvent", e.toString()); 
     } 
    } 
} 

を次のコードを使用しています、それはログ

E/onAccessibilityEvent﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RemoteViews.getLayoutId()'on a null object reference 

ノートに次のエラーが表示されます。 私は追加していません特定のアクセス許可。 エラーで私を手伝ってください。私は特にこれに新しいです。

+0

http://stackoverflow.com/questions/28980078/read-notification-bar-title-message-using-accessibility-service-programmaticallこれを確認してください。 – khetanrajesh

+0

私はマニフェストの中にパーミッションが必要かどうかを質問したいと思います。 – Aditya

答えて

0

コードは私の作品の下に、これを試してみてください -

Notification notification = (Notification) event.getParcelableData(); 
RemoteViews views = notification.contentView; 
Class secretClass = views.getClass(); 

try { 
Map<Integer, String> text = new HashMap<Integer, String>(); 

Field outerFields[] = secretClass.getDeclaredFields(); 
for (int i = 0; i < outerFields.length; i++) { 
    if (!outerFields[i].getName().equals("mActions")) continue; 

    outerFields[i].setAccessible(true); 

    ArrayList<Object> actions = (ArrayList<Object>) outerFields[i] 
      .get(views); 
    for (Object action : actions) { 
     Field innerFields[] = action.getClass().getDeclaredFields(); 

     Object value = null; 
     Integer type = null; 
     Integer viewId = null; 
     for (Field field : innerFields) { 
      field.setAccessible(true); 
      if (field.getName().equals("value")) { 
       value = field.get(action); 
      } else if (field.getName().equals("type")) { 
       type = field.getInt(action); 
      } else if (field.getName().equals("viewId")) { 
       viewId = field.getInt(action); 
      } 
     } 

     if (type == 9 || type == 10) { 
      text.put(viewId, value.toString()); 
     } 
    } 

    System.out.println("title is: " + text.get(16908310)); 
    System.out.println("info is: " + text.get(16909082)); 
    System.out.println("text is: " + text.get(16908358)); 
    } 
    } catch (Exception e) { 
e.printStackTrace(); 
} 

は、それはあなたを助けることを願っています。

あなたのresフォルダ内のxmlという名前のフォルダを作成する - という名前のそれのXML作成 - 「accessibilityserviceを」と、コードの下に貼り付ける -

<?xml version="1.0" encoding="utf-8"?> 
<accessibility-service 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:accessibilityEventTypes="typeNotificationStateChanged" 
android:accessibilityFeedbackType="feedbackSpoken" 
android:notificationTimeout="100" /> 

と内部をコードの下にサービスタグをアップデートマニフェスト -

<service 
    android:name=".YourServiceClassName" 
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" 
    > 
    <intent-filter> 
     <action 
    android:name="android.accessibilityservice.AccessibilityService" /> 
    </intent-filter> 

    <meta-data 
     android:name="android.accessibilityservice" 
     android:resource="@xml/accessibilityservice" /> 
</service> 
関連する問題