價(jià)格低的車(chē)百度關(guān)鍵詞seo排名
目錄
- 1、說(shuō)明
- 2、使用方法
- 2.1 常用方法
- 2.2 調(diào)用系統(tǒng)應(yīng)用
- 3、參考資料
1、說(shuō)明
在A(yíng)ndroid開(kāi)發(fā)中常常會(huì)用到Intent進(jìn)行不同活動(dòng)啟動(dòng),整理資料如下
2、使用方法
2.1 常用方法
1、一般情況而言,都是使用如下的方式進(jìn)行調(diào)用
Intent intent = new Intent(this, typeof(UpLoadService));
intent.PutExtra("serviceType", "once");
StartService(intent);
也存在調(diào)用第三方的情況
Intent intent = new Intent("android.intent.action.MAIN");
intent.setClassName("當(dāng)前Act的全限定類(lèi)名","啟動(dòng)Act的全限定類(lèi)名");
startActivity(intent);
具體可參考這篇文章:Android 啟動(dòng)一個(gè)Activity的幾種方式
2、傳遞數(shù)組參數(shù)
//加載
var intent = new Intent(this, typeof(TranslationHistoryActivity));
intent.PutStringArrayListExtra("phone_numbers", phoneNumbers);
StartActivity(intent);//獲取數(shù)據(jù)---在某個(gè)Activity中獲取數(shù)據(jù)
protected override void OnCreate(Bundle bundle)
{base.OnCreate(bundle);// Create your application herevar phoneNumbers = Intent.Extras.GetStringArrayList("phone_numbers") ?? new string[0];this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, phoneNumbers);
}
3、使用Bundle
傳遞
//加載
public static MineFragment NewInstance(MainActivity mainActivity, string employeeID,string employeeName,string loginTime)
{var frag1 = new MineFragment(mainActivity);Bundle bundle = new Bundle();bundle.PutString("id", employeeID);bundle.PutString ("name", employeeName);bundle.PutString("loginTime", loginTime);frag1.Arguments = bundle;return frag1;
}//獲取時(shí)使用
string employeeID = Arguments.GetString("id", "");
string employeeName = Arguments.GetString("name", "");
string loginTime = Arguments.GetString("loginTime", "");
4、在A(yíng)ctivity中使用
加載數(shù)據(jù)
//加載數(shù)據(jù)
Bundle bundle = new Bundle(); //得到bundle對(duì)象
bundle.putString("sff", "value值"); //key-"sff",通過(guò)key得到value-"value值"(String型)
bundle.putInt("iff", 175); //key-"iff",value-175
intent.putExtras(bundle); //通過(guò)intent將bundle傳到另個(gè)Activity
startActivity(intent);//讀取數(shù)據(jù)
Bundle bundle = this.getIntent().getExtras(); //讀取intent的數(shù)據(jù)給bundle對(duì)象
String str1 = bundle.getString("sff"); //通過(guò)key得到value
int int1 = bundle.getInt("iff");
5、使用Handler
//加載數(shù)據(jù)
Message message=new Message();//new一個(gè)Message對(duì)象
message.what = MESSAGE_WHAT_2;//給消息做標(biāo)記
Bundle bundle = new Bundle(); //得到Bundle對(duì)象
bundle.putString("text1","消息傳遞參數(shù)!"); //往Bundle中存放數(shù)據(jù)
bundle.putInt("text2",44); //往Bundle中put數(shù)據(jù)
message.setData(bundle);//mes利用Bundle傳遞數(shù)據(jù)
mHandler.sendMessage(message);//Handler將消息放入消息隊(duì)列//讀取數(shù)據(jù)
String str1=msg.getData().getString("text1");
int int1=msg.getData().getString("text2");
2.2 調(diào)用系統(tǒng)應(yīng)用
有時(shí)需要調(diào)用系統(tǒng)的應(yīng)用,例如發(fā)短信、打電話(huà)之類(lèi),則需要指定Action
。
Intent有很多overload形式,以下兩種比較常用:
public Intent(String action)
public Intent(String action, Uri uri)
例如撥打電話(huà):
//Java寫(xiě)法
Uri uri = Uri.parse("tel:10086");
// 參數(shù)分別為調(diào)用撥打電話(huà)組件的Action和獲取Data數(shù)據(jù)的Uri
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent); //Xamarin寫(xiě)法
var intent = new Intent(Intent.ActionCall);
intent.SetData(Android.Net.Uri.Parse("tel:" + Intent.GetStringExtra("phoneNumber")));
StartActivity(intent);
Intent調(diào)用常見(jiàn)系統(tǒng)組件有如下:
// 調(diào)用瀏覽器
Uri webViewUri = Uri.parse("http://blog.csdn.net/zuolongsnail");
Intent intent = new Intent(Intent.ACTION_VIEW, webViewUri); // 調(diào)用地圖
Uri mapUri = Uri.parse("geo:100,100");
Intent intent = new Intent(Intent.ACTION_VIEW, mapUri); // 播放mp3
Uri playUri = Uri.parse("file:///sdcard/test.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, playUri);
intent.setDataAndType(playUri, "audio/mp3"); // 調(diào)用撥打電話(huà)
Uri dialUri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_DIAL, dialUri);
// 直接撥打電話(huà),需要加上權(quán)限<uses-permission id="android.permission.CALL_PHONE" />
Uri callUri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_CALL, callUri); // 調(diào)用發(fā)郵件(這里要事先配置好的系統(tǒng)Email,否則是調(diào)不出發(fā)郵件界面的)
Uri emailUri = Uri.parse("mailto:zuolongsnail@163.com");
Intent intent = new Intent(Intent.ACTION_SENDTO, emailUri);
// 直接發(fā)郵件
Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = { "zuolongsnail@gmail.com" };
String[] ccs = { "zuolongsnail@163.com" };
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "the email text");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.setType("text/plain");
Intent.createChooser(intent, "Choose Email Client"); // 發(fā)短信
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("sms_body", "the sms text");
intent.setType("vnd.android-dir/mms-sms");
// 直接發(fā)短信
Uri smsToUri = Uri.parse("smsto:10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
intent.putExtra("sms_body", "the sms text");
// 發(fā)彩信
Uri mmsUri = Uri.parse("content://media/external/images/media/23");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "the sms text");
intent.putExtra(Intent.EXTRA_STREAM, mmsUri);
intent.setType("image/png"); // 卸載應(yīng)用
Uri uninstallUri = Uri.fromParts("package", "com.app.test", null);
Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri);
// 安裝應(yīng)用
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/test.apk"), "application/vnd.android.package-archive"); // 在A(yíng)ndroid Market中查找應(yīng)用
Uri uri = Uri.parse("market://search?q=憤怒的小鳥(niǎo)");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
3、參考資料
1、Android應(yīng)用開(kāi)發(fā)中Intent的作用及使用方法
2、Android中Bundle