view.reapply远程视图的bigContentView通知崩溃

时间:2014-01-26 18:28:10

标签: android android-notifications layout-inflater android-remoteview

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

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

我使用的一些信息来源: Extract notification text from parcelable, contentView or 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;
}

谢谢。

1 个答案:

答案 0 :(得分:0)

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