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
- C# datatable中重複資料去重C#
- sqlserver中刪除重複資料SQLServer
- sql重複插入問題SQL
- vue 解決不能攜帶session問題VueSession
- Androidstudio中jar包重複或jar包裡的類重複問題AndroidJAR
- PostgreSQL刪除表中重複資料SQL
- Java 去掉字串中的重複資料Java字串
- EXP匯出資料檔名稱重複問題的解決
- android包重複問題Android
- 處理網站中的重複內容問題網站
- jquery中事件重複繫結以及解綁問題jQuery事件
- C# 移除陣列中重複資料C#陣列
- Oracle中刪除重複資料的SqlOracleSQL
- mysql 刪除表中重複的資料MySql
- 小心避坑:MySQL分頁時出現的資料重複問題MySql
- 靈活運用分散式鎖解決資料重複插入問題分散式
- 有重複元素的排列問題
- db2中刪除重複記錄的問題DB2
- RabbitMQ如何解決被重複消費和資料丟失的問題?MQ
- scroll pagination.js資料重複載入、分頁問題JS
- 刪除重複資料
- mysql 清除重複資料MySql
- js 中基礎資料結構陣列去重問題JS資料結構陣列
- webpack dll打包重複問題優化Web優化
- java去掉陣列中重複的資料和遍歷資料Java陣列
- 重複資料刪除:備份中的“消重加速器”
- 【java】ObjectOutputStream & ObjectInputStream 多次寫入發生重複寫入相同資料的問題JavaObject
- 使用nohup執行迴圈指令碼插入發現資料重複的問題指令碼
- MySQL資料庫行去重複和列去重複MySql資料庫
- dataset 判斷整列是否有重複,找出重複資料
- mysql查詢表裡的重複資料方法和刪除重複資料MySql
- 使用Java Stream API中DistinctBy刪除重複資料JavaAPI
- oracle 查詢及刪除表中重複資料Oracle
- mysql 查詢及 刪除表中重複資料MySql
- 使用tar命令從磁帶中解壓資料夾的問題