2018年10月13日 星期六

[ Android Studio ] 使用Intent轉換Activity或Service並傳送資料的方法

使用Intent轉換Activity或Service並傳送資料


一、轉換

Intent intent = new Intent(this, 轉換類別名稱.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//如果這Activity是開啟的就不再重複開啟
startService(intent);




二、簡單傳送資料

Intent intent = new Intent(this, 轉換類別名稱.class);
intent.putExtra("B1", bmi);
startActivity(intent);


三、傳送資料方法一:使用了Bundle

Intent intent = new Intent(this, 轉換類別名稱.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//如果這Activity是開啟的就不再重複開啟
Bundle bundle = new Bundle();
bundle.putBoolean("flag", true);//傳遞Boolean值
bundle.putInt("H1", 50);//傳遞Int值
intent.putExtras(bundle);
startService(intent);

接收資料如下
Intent intent = getIntent();
Boolean flag = intent.getExtras().getBoolean("flag");
int H1 = intent.getExtras().getInt("H1");

四、附帶資料的方法:

用法:intent.getFloatExtra("B1", 0);

方法名稱 說明
getFloatExtra 取得float型態資料
getIntExtra 取得int型態資料
getLongExtra 取得long型態資料
getBooleanExtra 取得boolean型態資料
getCharExtra 取得char型態資料
getStringExtra 取得String型態資料
getStringArrayExtra 取得String陣列型態資料
以上都是取得對應陣列的方法
getStringArrayListExtra 取得內含String的List集合資料
上方是取得對應集合的方法


官網:https://developer.android.com/guide/components/intents-filters?hl=zh-tw






沒有留言:

張貼留言

影片的問題請留在影片的留言區裡。
部落格不會另外通知給我,所以很難發現你有留言。