第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將參數(shù)從通知單擊發(fā)送到活動?

如何將參數(shù)從通知單擊發(fā)送到活動?

達(dá)令說 2019-10-14 14:23:44
我可以找到一種從通知中向活動發(fā)送參數(shù)的方法。我有一個創(chuàng)建通知的服務(wù)。當(dāng)用戶單擊通知時,我想使用一些特殊參數(shù)打開我的主要活動。例如,一個項目ID,這樣我的活動就可以加載并顯示一個特殊的項目詳細(xì)信息視圖。更具體地說,我正在下載文件,下載文件時,我希望通知具有以下意圖:單擊該通知后,它將以特殊模式打開我的活動。我嘗試使用putExtra我的意圖,但似乎無法提取它,因此我認(rèn)為我做錯了。我的服務(wù)中創(chuàng)建通知的代碼:        // construct the Notification object.     final Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());    final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);    contentView.setImageViewResource(R.id.image, R.drawable.icon);    contentView.setTextViewText(R.id.text, tickerText);    contentView.setProgressBar(R.id.progress,100,0, false);    notif.contentView = contentView;            Intent notificationIntent = new Intent(context, Main.class);    notificationIntent.putExtra("item_id", "1001"); // <-- HERE I PUT THE EXTRA VALUE    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);    notif.contentIntent = contentIntent;    nm.notify(id, notif);我的活動中的代碼試圖從通知中獲取額外的參數(shù): public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    Bundle extras = getIntent().getExtras();    if(extras != null){        Log.i( "dd","Extra:" + extras.getString("item_id") );    }Extras始終為null,并且我從不記錄任何內(nèi)容。順便說一句... onCreate僅在我的活動開始時運行,如果我的活動已經(jīng)開始,我還想收集額外的物品,并根據(jù)收到的item_id介紹我的活動。有任何想法嗎?
查看完整描述

3 回答

?
一只名叫tom的貓

TA貢獻(xiàn)1906條經(jīng)驗 獲得超3個贊

我遇到類似的問題,我的應(yīng)用程序顯示消息通知。當(dāng)有多個通知并單擊每個通知時,它將在查看消息活動中顯示該通知詳細(xì)信息。我解決了在視圖消息意圖中接收到相同額外參數(shù)的問題。


這是修復(fù)此問題的代碼。用于創(chuàng)建通知意圖的代碼。


 Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);

    notificationIntent.putExtra("NotificationMessage", notificationMessage);

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);

查看消息活動的代碼。


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    onNewIntent(getIntent());

}


@Override

public void onNewIntent(Intent intent){

    Bundle extras = intent.getExtras();

    if(extras != null){

        if(extras.containsKey("NotificationMessage"))

        {

            setContentView(R.layout.viewmain);

            // extract the extra-data in the Notification

            String msg = extras.getString("NotificationMessage");

            txtView = (TextView) findViewById(R.id.txtMessage);

            txtView.setText(msg);

        }

    }



}


查看完整回答
反對 回復(fù) 2019-10-14
  • 3 回答
  • 0 關(guān)注
  • 391 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號