在应用

时间:2015-08-29 11:27:00

标签: android

我有一个运行服务的应用程序会监听通知,但它显示所有通知,我只想显示whatsapp通知,所以我检查我的包是否等于com.whatsapp但是它没有显示什么都没有,代码:

    private BroadcastReceiver onNotice= new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String pack = intent.getStringExtra("package");
            String title = intent.getStringExtra("title");
            String text = intent.getStringExtra("text");
        if (pack == "com.whatsapp") { 
                TableRow tr = new TableRow(getApplicationContext());
                tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                TextView textview = new TextView(getApplicationContext());
                textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
                textview.setTextSize(20);
                textview.setTextColor(Color.parseColor("#0B0719"));
                textview.setText(Html.fromHtml(pack + "<br><b>" + title + " : </b>" + text));
                tr.addView(textview);
                tab.addView(tr);
            }
        }
    };
}
我正在检查它是否等于错?

如果需要,服务代码:

@Override

public void onNotificationPosted(StatusBarNotification sbn) {
    String pack = sbn.getPackageName();
    String ticker = sbn.getNotification().tickerText.toString();
    Bundle extras = sbn.getNotification().extras;
    String title = extras.getString("android.title");
    String text = extras.getCharSequence("android.text").toString();
    Log.i("Package",pack);
    Log.i("Ticker",ticker);
    Log.i("Title",title);
    Log.i("Text",text);
    Intent msgrcv = new Intent("Msg");
    msgrcv.putExtra("package", pack);
    msgrcv.putExtra("ticker", ticker);
    msgrcv.putExtra("title", title);
    msgrcv.putExtra("text", text);
    LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}

注意:当我删除if语句时,一切正常,它显示com.whatsapp作为包。谢谢你们

2 个答案:

答案 0 :(得分:1)

你必须use the equals() method to compare Strings==仅比较他们的参考,而不是他们的价值观。

if ("com.whatsapp".equals(pack)) {
    // ...
}

答案 1 :(得分:0)

在发送广播之前检查包名称。

if ("com.whatsapp".equals(pack)) {
  LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}