Notification中Intent攜帶資料重複問題
Notification中多個Intent使用putExtra方法攜帶資料,在目的activity中通過getIntent方法取出傳遞資料時,有時候會發現取出來的資料都是第一次putExtra中放入的資料。
下面看下程式碼,比較簡單:
package com.example.notificationdemo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity {
private Button mButton;
private NotificationManager mNotificationManager;
private Intent mIntent;
private PendingIntent mPendingIntent;
Notification mNotification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
mNotification = new Notification();
mIntent = new Intent(FirstActivity.this, MainActivity.class);
mIntent.putExtra("hello", "first");
Log.v("@@@@@@", "this is put in intent first....");
mNotificationManager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//主要在於PendingIntent的getActivity方法中的引數
mPendingIntent = PendingIntent.getActivity(FirstActivity.this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//如果使用下面註釋掉的程式碼,將會出現上面說講到的問題,當然在SecondActivity中也必須做修改
//mPendingIntent = PendingIntent.getActivity(FirstActivity.this, 0, mIntent, 0);
mButton = (Button)findViewById(R.id.first_button);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
showNotification();
}
});
}
private void showNotification() {
mNotification.icon = R.drawable.message;
mNotification.defaults = Notification.DEFAULT_SOUND;
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotification.tickerText = "第一個";
mNotification.setLatestEventInfo(FirstActivity.this, "第一個",
"第一個", mPendingIntent);
mNotificationManager.notify(0, mNotification);
}
}
第二個Activity:
package com.example.notificationdemo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity {
private Button mButton;
private NotificationManager mNotificationManager2;
private Intent mIntent;
private PendingIntent mPendingIntent2;
Notification mNotification2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
mNotification2 = new Notification();
mIntent = new Intent(SecondActivity.this, MainActivity.class);
mIntent.putExtra("hello", "second");
Log.v("@@@@@@", "this is doing in second activity");
mNotificationManager2 = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mPendingIntent2 = PendingIntent.getActivity(SecondActivity.this, 1, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//如果使用下面註釋掉的程式碼,將會出現上面說講到的問題,當然在FirstActivity中也必須做修改(把上面程式碼註釋掉,使用下面行程式碼,就會重現)
//mPendingIntent2 = PendingIntent.getActivity(SecondActivity.this, 0, mIntent, 0);
mButton = (Button) findViewById(R.id.second_button);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
showNotification();
}
});
}
private void showNotification() {
mNotification2.icon = R.drawable.message2;
mNotification2.defaults = Notification.DEFAULT_SOUND;
mNotification2.flags |= Notification.FLAG_AUTO_CANCEL;
mNotification2.tickerText = "第二個";
Log.v("@@@@@@", "intent put second ...");
mNotification2.setLatestEventInfo(SecondActivity.this, "第二個", "第二個", mPendingIntent2);
Log.v("@@@@@@", "do shownotification in second activity");
mNotificationManager2.notify(0, mNotification2);
}
}
在MainActivity中接受兩個Activity中的Notification傳過來的資料:
package com.example.notificationdemo;
import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
public class MainActivity extends TabActivity implements OnCheckedChangeListener{
private TabHost mTabHost;
private RadioGroup mRadioGroup;
private RadioButton mFirstRadio, mSecondRadio;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new Intent(this, FirstActivity.class)));
mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new Intent(this, SecondActivity.class)));
mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
mRadioGroup.setOnCheckedChangeListener(this);
mFirstRadio = (RadioButton)findViewById(R.id.first_radio);
mSecondRadio = (RadioButton) findViewById(R.id.second_radio);
Log.v("@@@@@@", "this is doing in onCreate");
ChangeChecked(true);
String str = getIntent().getStringExtra("hello");
Log.v("@@@@@@", "the str is " + str);
if (str != null && str.equals("first")) {
ChangeChecked(true);
mTabHost.setCurrentTabByTag("first");
} else if (str != null && str.equals("second")) {
ChangeChecked(false);
mTabHost.setCurrentTabByTag("second");
}
}
@Override
protected void onNewIntent(Intent intent) {
String str = intent.getStringExtra("hello");
if (str != null && str.equals("first")) {
ChangeChecked(true);
mTabHost.setCurrentTabByTag("first");
} else if (str != null && str.equals("second")) {
ChangeChecked(false);
mTabHost.setCurrentTabByTag("second");
}
super.onNewIntent(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.first_radio:
mTabHost.setCurrentTabByTag("first");
ChangeChecked(true);
break;
case R.id.second_radio:
mTabHost.setCurrentTabByTag("second");
ChangeChecked(false);
break;
default :
break;
}
}
private void ChangeChecked(boolean bool) {
if (bool) {
mFirstRadio.setTextColor(Color.RED);
mFirstRadio.setBackgroundResource(R.drawable.tab_front_bg);
mSecondRadio.setTextColor(Color.BLACK);
mSecondRadio.setBackgroundColor(Color.TRANSPARENT);
} else {
mSecondRadio.setTextColor(Color.RED);
mSecondRadio.setBackgroundResource(R.drawable.tab_front_bg);
mFirstRadio.setTextColor(Color.BLACK);
mFirstRadio.setBackgroundColor(Color.TRANSPARENT);
}
}
}
問題主要出自:
mPendingIntent2 = PendingIntent.getActivity(SecondActivity.this, 1, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//如果使用下面註釋掉的程式碼,將會出現上面說講到的問題,當然在FirstActivity中也必須做修改(把上面程式碼註釋掉,使用下面行程式碼,就會重現)
//mPendingIntent2 = PendingIntent.getActivity(SecondActivity.this, 0, mIntent, 0);
看一下getActivity方法:
PendingIntent android.app.PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.
Parameters:
context The Context in which this PendingIntent should start the activity.
requestCode Private request code for the sender (currently not used).
intent Intent of the activity to be launched.
flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
Returns:
Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.
關於該方法的詳細概述,敬請檢視後面關於notification的原始碼解析,打算在週末的時候看一下notification原始碼,那個時候再詳細分析下。
相關文章
- 安卓開發學習-Intent攜帶資料安卓Intent
- 資料檢視的重複問題
- MySQL order by limit 分頁資料重複問題MySqlMIT
- sqlserver中刪除重複資料SQLServer
- vue 解決不能攜帶session問題VueSession
- PostgreSQL刪除表中重複資料SQL
- 小心避坑:MySQL分頁時出現的資料重複問題MySql
- 靈活運用分散式鎖解決資料重複插入問題分散式
- Oracle中刪除表中的重複資料Oracle
- Android studio glide包重複問題AndroidIDE
- RabbitMQ如何解決被重複消費和資料丟失的問題?MQ
- mysql 刪除表中重複的資料MySql
- mysql 清除重複資料MySql
- 刪除重複資料
- webpack dll打包重複問題優化Web優化
- element UI元件樣式重複問題UI元件
- 【java】ObjectOutputStream & ObjectInputStream 多次寫入發生重複寫入相同資料的問題JavaObject
- js 中基礎資料結構陣列去重問題JS資料結構陣列
- LeetCode 26 號問題 刪除陣列中的重複項LeetCode陣列
- dataset 判斷整列是否有重複,找出重複資料
- 併發請求的重複插入問題
- 解決winform窗體重複建立問題ORM
- mongodb刪除重複資料MongoDB
- mysql避免插入重複資料MySql
- MySQL 處理重複資料MySql
- mongodb如何去除重複資料MongoDB
- 資料處理之去除重複資料
- 使用Java Stream API中DistinctBy刪除重複資料JavaAPI
- kafka 消費組功能驗證以及消費者資料重複資料丟失問題說明 3Kafka
- php資料庫資料如何去除重複資料呢?PHP資料庫
- Vue echarts 繫結事件重複執行問題VueEcharts事件
- 快速解決mongodb出現id重複問題MongoDB
- 去除重複字母(不同字元的最小序列)問題字元
- Oracle:重複資料去重,只取最新的一條資料Oracle
- Dynamics CRM 資料匯出到Excel時列標題不能重複Excel
- Leetcode--442. 陣列中重複的資料(JS版)LeetCode陣列JS
- MySQL 查詢重複的資料MySql
- Android Intent 傳遞資料大小限制AndroidIntent
- 答讀者問:關於隱式 id 重複的問題