1

我正在尝试构建一个应用程序来向我的智能手表发布通知。我找到了一个很棒的通知示例应用程序,我已经对其进行了修改以进行测试,它可以按预期工作。我发现我需要显示的扩展信息在 RemoteViews bigContentView 中。我找到了一个名为 Botification 的程序,它能够放大我正在寻找的视图。它在他的应用程序中按预期工作。我正在尝试将他的方法与我的方法结合起来,但没有运气。我还找到了 Notify Me! 的一些来源,它使用相同类型的方法来拉取通知远程视图。

这是我的第一个真正的 Android 应用程序,我是 Java 新手。我已经工作了大约一个月,并且从这个网站上的帖子和浏览网页中学到了很多东西。我浏览了 Lynda.com 的 Java Essentials 和 Android Essentials 课程。还发现这对我很有用,因为是 Java 新手,并且在 C++ 方面只有一点背景:http: //chortle.ccsu.edu/java5/index.html

我使用的一些信息来源: 从 parcelable、contentView 或 contentIntenthttp://www.kpbird.com/2013/07/android-notificationlistenerservice.html中提取通知文本

任何帮助,将不胜感激。Botification 应用程序使用处理程序和不同的方式来处理 onPosted 请求,这让我想知道这是否是我的问题。任何可以使我的大脑发胖的文档或教程都将受到欢迎。

我包括了使用此远程视图功能的代码部分,并希望足以说明我如何使其工作。

MainActivity.java 公共类 MainActivity 扩展 Activity {

private TextView txtView;
private NotificationReceiver nReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtView = (TextView) findViewById(R.id.textView);
    nReceiver = new NotificationReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.example.snotify.NOTIFICATION_LISTENER");
    registerReceiver(nReceiver,filter);
}

nlservice.java 公共类 NLService 扩展 NotificationListenerService {

private String TAG = "NLService";
private NLServiceReceiver nlreceiver;

@Override
public void onCreate() {
    super.onCreate();
    nlreceiver = new NLServiceReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.example.snotify.NOTIFICATION_LISTENER_SERVICE");
    registerReceiver(nlreceiver,filter);
}


@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(nlreceiver);
}


@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Notification n = sbn.getNotification();
    Log.i(TAG,"!!!onNotificationPosted");
    Log.i(TAG,"ID :" + sbn.getId() + "Ticker:" + n.tickerText + "Pkg:" + sbn.getPackageName());
    String text = NotiCrunch.extractTextFromNotification(NLService.this, n);
    String description = "";
    if (n.tickerText != null) {
        description = n.tickerText.toString();
    }
    Intent i = new  Intent("com.example.snotify.NOTIFICATION_LISTENER");
    i.putExtra("notification_event","Notification Posted\nPackage:" + sbn.getPackageName() + "\nID:" + sbn.getId() 
            + "\nText:" + text
            + "\nTag:" + sbn.getTag() + "\nTicker:" + description + "\n\n");
    sendBroadcast(i);

}

noticrunch.java

公共类 NotiCrunch {

private static String TAG = "NotiCrunch";
private static final int TIMESTAMPID = 16908388;

private static void extractViewType(ArrayList<View> outViews, Class<TextView> viewtype, View source) {
    if (ViewGroup.class.isInstance(source)) {
        ViewGroup vg = (ViewGroup) source;
        for (int i = 0; i < vg.getChildCount(); i++) {
            extractViewType(outViews, viewtype, vg.getChildAt(i));

        }
    } else if(viewtype.isInstance(source)) {
        outViews.add(source);
    }
}

public static String extractTextFromNotification(Service service, Notification notification) {
    ArrayList<String> result = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        result = extractTextFromNotification(service, notification.bigContentView);
        Log.d(TAG, "It tried Big View");
        Log.d(TAG, "Ticker:" + notification.tickerText);
    }
    if (result == null) {
        result = extractTextFromNotification(service, notification.contentView);
        Log.d(TAG, "It tried little view");
    }
    if (result == null){
        Log.d(TAG, "It is returning null");
        return "";
    }
    return TextUtils.join("\n", result);

}

private static ArrayList<String> extractTextFromNotification(Service service, RemoteViews view) {
    LayoutInflater inflater = (LayoutInflater) service.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ArrayList<String> result = new ArrayList<String>();
    if (view == null) {
        Log.d(TAG, "Initial view is Empty");
        return null;
    }
    try {
        ViewGroup localView = (ViewGroup) inflater.inflate(view.getLayoutId(), null);
        view.reapply(service.getApplicationContext(), localView);
        ArrayList<View> outViews = new ArrayList<View>();
        extractViewType(outViews, TextView.class, localView);
        for (View  ttv: outViews) {
            TextView tv = (TextView) ttv;
            String txt = tv.getText().toString();
            if (!TextUtils.isEmpty(txt) && tv.getId() != TIMESTAMPID) {
                result.add(txt);
            }
        }
    } catch (Exception e) {
        Log.d(TAG, "FAILED to load notification! " + e.toString());
        Log.wtf(TAG, e);
        return null;
    }
    Log.d(TAG, "Return result" + result);
    return result;
}

谢谢你。

4

1 回答 1

0

代码在哪里崩溃?我在任何地方都没有看到重新申请的电话。

于 2014-01-29T16:48:59.790 回答