总结安卓(Android)中常用的跳转工具
话不多说了,直接上代码,这篇文章包含了一些基本的并且常用的跳转工具,一起来看看吧。
首先,这是需要的对应的权限。
<uses-permissionandroid:name="android.permission.CALL_PHONE"/> <uses-permissionandroid:name="android.permission.SEND_SMS"/> <uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/>
代码:
importandroid.app.Activity; importandroid.content.Context; importandroid.content.Intent; importandroid.database.Cursor; importandroid.net.Uri; importandroid.os.Environment; importandroid.provider.MediaStore; importandroid.provider.Settings; importandroid.support.v7.app.AppCompatActivity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.Toast; importjava.io.File; importframe.zmit.cn.publicutils.R; /** *Createdbyjoyon2016/7/21. *工具集合 */ publicclassMainActivityextendsAppCompatActivity{ privateStringmImagePath;//返回的图片路径 privatefinalstaticintREQUEST_CODE_GALLERY=0x11; privatefinalstaticintREQUEST_CODE_CAMERA=0x12; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** *打开微信 */ findViewById(R.id.btn_open_weixin).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Intentintent=getPackageManager().getLaunchIntentForPackage("com.tencent.mm"); startActivity(intent); } }); /** *跳转到QQ聊天界面 */ findViewById(R.id.btn_open_qq).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ StringmQqNumber="98*****08"; Stringurl11="mqqwpa://im/chat?chat_type=wpa&uin="+mQqNumber+"&version=10"; startActivity(newIntent(Intent.ACTION_VIEW,Uri.parse(url11))); } }); /** *跳转到联系人界面 */ findViewById(R.id.btn_people_interface).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Intentintent=newIntent(); intent.setClassName("com.android.contacts","com.android.contacts.activities.PeopleActivity"); startActivity(intent); } }); /** *跳转到拨号界面 */ findViewById(R.id.btn_telephone_interface).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Stringphone="157*****737"; Intentintent=newIntent(Intent.ACTION_DIAL,Uri.parse("tel:"+phone)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }); /** *直接拨打电话 */ findViewById(R.id.btn_telephone).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Stringphone="157*****737"; IntentintentPhone=newIntent(Intent.ACTION_CALL,Uri.parse("tel:"+phone)); startActivity(intentPhone); } }); /** *跳转到短信界面 */ findViewById(R.id.btn_message_interface).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Intentintent=newIntent(Intent.ACTION_VIEW); intent.setType("vnd.android-dir/mms-sms"); startActivity(intent); } }); /** *跳转到短信界面 *指定号码和短信内容 */ findViewById(R.id.btn_message).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Stringphone="157*****737"; Stringmessage="这是一个自定义的内容"; Intentintent=newIntent(Intent.ACTION_SENDTO,Uri.parse("smsto:"+phone)); intent.putExtra("sms_body",message); startActivity(intent); } }); /** *跳转到设置界面 */ findViewById(R.id.btn_setting_interface).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Intentintent=newIntent(Settings.ACTION_SETTINGS); startActivity(intent); } }); /** *跳转到蓝牙界面 */ findViewById(R.id.btn_bluetooth_interface).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Intentintent=newIntent(Settings.ACTION_BLUETOOTH_SETTINGS); startActivity(intent); } }); /** *调用浏览器 */ findViewById(R.id.btn_browser).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Intentintent=newIntent(); intent.setAction("android.intent.action.VIEW"); Uricontent_url=Uri.parse("http://www.baidu.com"); intent.setData(content_url); startActivity(intent); } }); /** *打开照相机 */ findViewById(R.id.btn_camera).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ mImagePath=chooseImageFromCamera(MainActivity.this,REQUEST_CODE_CAMERA,"cameraImage"); } }); /** *打开相册并获取图片地址 */ findViewById(R.id.btn_gallery).setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Intentintent=newIntent(Intent.ACTION_PICK,null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); startActivityForResult(intent,REQUEST_CODE_GALLERY); } }); } publicStringchooseImageFromCamera(Activityactivity,intrequestCode,StringimageName){ IntentcameraIntent=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); Filefile=newFile(Environment.getExternalStorageDirectory()+"/Images"); if(!file.exists()){ file.mkdirs(); } StringsavePath=Environment.getExternalStorageDirectory()+"/Images/"+imageName+String.valueOf(System.currentTimeMillis())+".jpg"; UrimUri=Uri.fromFile( newFile(savePath)); cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mUri); cameraIntent.putExtra("return-data",true); activity.startActivityForResult(cameraIntent,requestCode); returnsavePath; } publicStringonActivityResultForChooseImageFromGallery( Contextcontext,intrequestCode,intresultCode,Intentdata){ StringimagePath=null; if(data!=null){ if("file".equals(data.getData().getScheme())){ /**有些低版本机型返回的Uri模式为file*/ imagePath=data.getData().getPath(); }else{ UriselectedImage=data.getData(); String[]filePathColumn={MediaStore.Images.Media.DATA}; Cursorcursor=context.getContentResolver().query(selectedImage, filePathColumn,null,null,null); cursor.moveToFirst(); intcolumnIndex=cursor.getColumnIndex(filePathColumn[0]); imagePath=cursor.getString(columnIndex); cursor.close(); } returnimagePath; }else{ returnnull; } } @Override protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){ super.onActivityResult(requestCode,resultCode,data); switch(requestCode){ caseREQUEST_CODE_GALLERY: mImagePath=onActivityResultForChooseImageFromGallery(MainActivity.this, requestCode,resultCode,data); if(mImagePath!=null&&mImagePath.length()>0){ Toast.makeText(MainActivity.this,mImagePath,Toast.LENGTH_SHORT).show(); } break; caseREQUEST_CODE_CAMERA: if(mImagePath!=null&&mImagePath.length()>0){ Toast.makeText(MainActivity.this,mImagePath,Toast.LENGTH_SHORT).show(); } break; } } }
总结
总结安卓(Android)中常用的跳转工具到这就结束了,这篇文章整理的这些跳转工具类很常见,所以才是最实用的,希望本文对大家日常开发Android能有所帮助。