android系统自带的intent集锦
1、设置应用当前语言 /** *设置应用当前语言,如: lan=""(表示当前系统语言), lan=" zh"(中文), lan=" ja"(日语) * @paramlan */ publicvoidsetLanguage(Stringlan){ Localelocale= newLocale(lan); Locale. setDefault(locale); Configurationconfig= newConfiguration(); config.locale=locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } 2、重启Activity /** *重启Activity,很是巧妙 */ publicvoidreload(){
Intentintent=getIntent(); overridePendingTransition(0,0); intent.addFlags(Intent. FLAG_ACTIVITY_NO_ANIMATION); finish();
overridePendingTransition(0,0); startActivity(intent); } 3、更改系统亮度 /** *更改系统亮度 * @paramvalue */ publicvoidchangeBrightness( intvalue){ System. putInt(getContentResolver(),System. SCREEN_BRIGHTNESS,value); LayoutParamslayoutpars=getWindow().getAttributes(); layoutpars.screenBrightness=value/( float)255; getWindow().setAttributes(layoutpars); } 4、设置全屏 /** *设置全屏 */ protectedvoidsetFullScreen(){ getWindow().requestFeature(Window. FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN,WindowManager.LayoutParams. FLAG_FULLSCREEN); } 5、打开网页( recommended_url在strings.xml文件中配置的,如:http://www.baidu.com) ButtonbuttonRecommended=(Button)findViewById(R.id. buttonRecommended); buttonRecommended.setOnClickListener( newOnClickListener(){ @Override publicvoidonClick(Viewv){ finalIntentintent= newIntent(Intent. ACTION_VIEW,Uri. parse(getString(R.string. recommended_url))); startActivity(intent); } });
6、打开系统相册 privatevoidpicture(){ finalIntentimageIntent= newIntent(Intent. ACTION_PICK,android.provider.MediaStore.Images.Media. INTERNAL_CONTENT_URI); imageIntent.setType("image/*"); startActivityForResult(imageIntent, REQUEST_CODE_CAMERA); } 7、启动拍照 publicvoidstartCamera(){ filename=(imageList.size()+1)+".jpg"; pictureUri=Uri. fromFile( newFile(fileUri,filename)); if(sp== null) sp=getPreferences( MODE_PRIVATE); Editoreditor=sp.edit(); editor.putString("pictureUri",pictureUri.toString()); editor.commit(); Intentintent= newIntent(); intent.setAction("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore. EXTRA_OUTPUT,pictureUri); startActivityForResult(intent, REQUEST_CODE_CAMERA_SHOOT); }
/**
*照相功能
*/
privatevoidcameraMethod(){
IntentimageCaptureIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
strImgPath=Environment.getExternalStorageDirectory().toString()+"/CONSDCGMPIC/";//存放照片的文件夹
StringfileName=newSimpleDateFormat("yyyyMMddHHmmss").format(newDate())+".jpg";//照片命名
Fileout=newFile(strImgPath);
if(!out.exists()){
out.mkdirs();
}
out=newFile(strImgPath,fileName);
strImgPath=strImgPath+fileName;//该照片的绝对路径
Uriuri=Uri.fromFile(out);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT,uri);//指定照片存放路径,默认是在/mnt/sdcard/DICM/Camera/目录下
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
startActivityForResult(imageCaptureIntent,RESULT_CAPTURE_IMAGE);
}
8、启动录像 publicvoidstartVideo(){ filename="video_template"+System. currentTimeMillis()+".mp4"; videoUri=Uri. fromFile( newFile(fileUri,filename)); if(sp== null){ sp=getPreferences( MODE_PRIVATE); } Editoreditor=sp.edit(); editor.putString("videoUri",videoUri.toString()); IntentmIntent= null; mIntent= newIntent(); mIntent.setAction("android.media.action.VIDEO_CAPTURE"); //mIntent.putExtra(MediaStore.EXTRA_OUTPUT,videoUri); //mIntent=newIntent(MediaStore.ACTION_VIDEO_CAPTURE); //mIntent.putExtra(MediaStore.EXTRA_OUTPUT,videoUri);//指定照片存放路径,默认是在/mnt/sdcard/DICM/Camera/目录下, // //mIntent.putExtra("android.intent.extra.sizeLimit",999999999L); //mIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,0);//0表示lowQuality //mIntent.putExtra("android.intent.extra.durationLimit",3600); startActivityForResult(mIntent, REQUEST_CODE_VIDEO_SHOOT);}
9、拍摄视频
privatevoidvideoMethod(){
Intentintent=newIntent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,0);
startActivityForResult(intent,REQUEST_CODE_TAKE_VIDEO);
}
10、启动录音/**
*录音功能
*/
privatevoidsoundRecorderMethod(){
Intentintent=newIntent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/amr");
startActivityForResult(intent,RESULT_CAPTURE_RECORDER_SOUND);
}
11、取得系统中所有的Audio文件(AudioInfo是自定义的bean类): publicAudioInfogetMp3Info(StringaudioPath){ Stringselection=MediaStore.Audio.Media. DATA+"like?";//likeStringpath="/ mnt/ sdcard/music"; String[]selectionArgs={"%"+audioPath+"%"}; String[]projection={ //MediaStore.Audio.Media._ID, MediaStore.Audio.Media. TITLE, MediaStore.Audio.Media. ARTIST, MediaStore.Audio.Media. ALBUM, MediaStore.Audio.Media. DURATION, MediaStore.Audio.Media. SIZE, MediaStore.Audio.Media. DISPLAY_NAME, MediaStore.Audio.Media. DATA//-->FileFullPath }; Cursorcursor= null; cursor=managedQuery(MediaStore.Audio.Media. EXTERNAL_CONTENT_URI,projection,selection,selectionArgs, null); //cursor=managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,null,null,null);//selectallaudiofile //Cursorcursor2=query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER) AudioInfoaudioInfo= newAudioInfo(); for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){ audioInfo.setTitle(cursor.getString(0).toString()); audioInfo.setArtist(cursor.getString(1).toString()); audioInfo.setAlbum(cursor.getString(2).toString()); audioInfo.setDuration(cursor.getString(3).toString()); audioInfo.setSize(cursor.getString(4).toString()); audioInfo.setFilename(cursor.getString(5).toString()); audioInfo.setFilePath(cursor.getString(6).toString()); Log. e( LOG,audioInfo.toString()+","+cursor.getString(5).toString()+","+cursor.getString(6).toString()); } returnaudioInfo; }
注:取得视频文件的信息有点类似 12、发送电子邮件:
//youneedconfigthemailappinyourandroidmoblefirst,andthemailwillsendbythemailapp.andthereareonebigbug:
//youcan'tsendthemailSilentlyandyouneedtoclickthesendbutton
//第一种方法是调用了系统的mailapp,你首先要配置系统的mailapp,但是这个方法的最大问题是,你运行这个方法后它并不会默认的发送邮件,而是弹出mail的app界面,你需要手动的点击发送
publicintsendMailByIntent(){
String[]reciver=newString[]{"181712000@qq.com"};
String[]mySbuject=newString[]{"test"};
StringmyCc="cc";
Stringmybody="测试EmailIntent";
IntentmyIntent=newIntent(android.content.Intent.ACTION_SEND);
myIntent.setType("plain/text");
myIntent.putExtra(android.content.Intent.EXTRA_EMAIL,reciver);
myIntent.putExtra(android.content.Intent.EXTRA_CC,myCc);
myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,mySbuject);
myIntent.putExtra(android.content.Intent.EXTRA_TEXT,mybody);
startActivity(Intent.createChooser(myIntent,"mailtest"));
return1;
}
/*thismethodcan'tbeusedinandroidmobilesuccessful,butitcanrunnormallyinPC.
Becauseitwillcausethejava.lang.NoClassDefFoundError:javax.activation.DataHandlererror
Maybetherearesomewaytosoloveit......therearealwaysjavaxpackagenotfoundinandroidvirtualmobile.
Bytheway,themethoduseApachemailjar
*/
//第二种,是调用了apache的common库,在pc上可以正常运行,但是在android虚拟机中会报错java.lang.NoClassDefFoundError:javax.activation.DataHandlererror
//javax包无法找到,我看了下这个问题还是比较普遍的,大家普遍表示虚拟机是被阉割的版本,javax好像存在不全,这个实际上就无法运行
publicintsendMailByApache(){
try{
HtmlEmailemail=newHtmlEmail();
//这里是发送服务器的名字
email.setHostName("smtp.gmail.com");
//编码集的设置
email.setTLS(true);
email.setSSL(true);
email.setCharset("gbk");
//收件人的邮箱
email.addTo("181712000@qq.com");
//发送人的邮箱
email.setFrom("wcf0000@gmail.com");
//如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
email.setAuthentication("wcf1000","00000");
email.setSubject("测试EmailApache");
//要发送的信息
email.setMsg("测试EmailApache");
//发送
email.send();
}catch(EmailExceptione){
//TODOAuto-generatedcatchblock
Log.i("IcetestActivity",e.getMessage());
}
return1;
}
/*
*thismethodusejavamailforandroid,itisagoodjar,
*youcanseethedemoinhttp://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android
*andyoualsoneedthreejars,whichIofferedinattachement
*
**/
//第三种,实际是javamail有人做了移植,专门为android做了开发,这下就比较好了,网上的demo代码也比较到位,只有一个问题,就是要自己添加一个mail.java,而且对stmp要手动添加。
//其实理论上还应该有一种,自己实现smtp服务器,全程采用socket编程,直接与目标服务器交流,这个win下面我写过,但是android下就算了,而且长期来讲面临着smtp服务器以后会被进行方向查询,以提高安全性。
publicintsendMailByJavaMail(){
Mailm=newMail("wcfXXXX@gmail.com","XXXXX");
m.set_debuggable(true);
String[]toArr={"18170000@qq.com"};
m.set_to(toArr);
m.set_from("18170000@qq.com");
m.set_subject("ThisisanemailsentusingicetestfromanAndroiddevice");
m.setBody("Emailbody.testbyJavaMail");
try{
//m.addAttachment("/sdcard/filelocation");
if(m.send()){
Log.i("IcetestActivity","Emailwassentsuccessfully.");
}else{
Log.i("IcetestActivity","Emailwassentfailed.");
}
}catch(Exceptione){
//Toast.makeText(MailApp.this,
//"Therewasaproblemsendingtheemail.",
//Toast.LENGTH_LONG).show();
Log.e("MailApp","Couldnotsendemail",e);
}
return1;
}
}
13、打电话发短信: 调用打电话和发短信功能(ANDROID)1.打电话
可以自己写界面,在button的单击事件中添加如下代码即可:
Intentintent=newIntent();
intent.setAction("android.intent.action.CALL");
intent.setData(Uri.parse("tel:"+mobile));//mobile为你要拨打的电话号码,模拟器中为模拟器编号也可
startActivity(intent);
或者
IntentmyIntentDial=newIntent(Intent.ACTION_CALL,Uri.parse("tel:"+mobile));
startActivity(myIntentDial); 调用拨打电话界面:
Intentintent=newIntent();
intent.setAction("android.intent.action.DIAL");
intent.setData(Uri.parse("tel:"+mobile));//mobile为你要拨打的电话号码,模拟器中为模拟器编号也可
startActivity(intent); 或者 IntentmyIntentDial=newIntent(Intent.ACTION_DIAL,Uri.parse("tel:"+mobile)); startActivity(myIntentDial); 需要添加打电话权限:
<uses-permissionAndroid:name="android.permission.CALL_PHONE"/>
14.发短信
和打电话差不多,在button的单击事件中添加如下代码:
SmsManagersmsManager=SmsManager.getDefault();
ArrayList<String>texts=smsManager.divideMessage(content);//拆分短信,短信字数太多了的时候要分几次发
for(Stringtext:texts){
smsManager.sendTextMessage(mobile,null,text,null,null);//发送短信,mobile是对方手机号
}
对应发短信权限:
<uses-permissionAndroid:name="android.permission.SEND_SMS"/>
附:调用系统发送短信和已发送短信界面
a.调用系统发送短信界面(并指定短信接收人和短信内容)
UrismsToUri=Uri.parse("smsto:10086");
IntentmIntent=newIntent(android.content.Intent.ACTION_SENDTO,smsToUri);
mIntent.putExtra("sms_body","TheSMStext");
startActivity(mIntent);
b.调用系统已发送短信界面
UrismsUri=Uri.parse("smsto:106900867734");
Intentintent=newIntent(Intent.ACTION_MAIN,smsUri);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
15、全屏、亮度、模糊度设置 //设置窗体全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置窗体始终点亮
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//设置窗体背景模糊
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
更多相关文章
- Android(安卓)build/envsetup.sh分析
- 解决scrollview中内容改变后自动滑动到底部的问题
- Android之Fragments
- ionic上拉翻页及下拉刷新
- Android之权限(permission)大全
- 安卓 Android之开发简单小应用(三)
- Android(SpreadTurm)开锁前设置一个亮度,解锁后再设置一个亮度
- Android材料设计库之折叠式布局你应该知道的一切
- Android(安卓)关机实现