Android常用功能代码块
1、设置activity无标题,全屏
// 设置为无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏模式 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
2、获得屏幕高度和宽度
//获取屏幕的高度和宽度用到WindowManager这个类 WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight();
3、获取手机各种信息
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();//移动设备国际辨识码
String imsi = tm.getSubscriberId();//国际移动用户识别码
String tel = tm.getLine1Number();//电话号码
String model = android.os.Build.MODEL;//手机型号
String sdk = android.os.Build.VERSION.SDK;//SDK版本
String release = android.os.Build.VERSION.RELEASE;//系统版本
//根据IMSI号码识别移动供应商
public String getProvidersName(String IMSI) {
String ProvidersName = null;
// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中国移动";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中国联通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中国电信";
}
return ProvidersName;
}
4、使用Toast输出一个字符串
public void showToast(String text){
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
5、把一个字符串写进文件
//把一个字符串写进文件
public void writeFile(String str,String path){
File file;
FileOutputStream out;
try{
//创建文件
file = new File(path);
file.createNewFile();
//打开文件file的输出流
out = new FileOutputStream(file);
//将字符串转换成byte数组写入文件
out.write(str.getBytes());
out.close();
}catch(IOException e){
}
}
6、把文件内容读出到字符串
//把文件内容读出到字符串
public String getFileInfo(String path){
File file;
String str = "";
FileInputStream in;
try{
//打开文件的inputStream
file new File(path);
in = new FileInputStream(file);
//将文件内容读入byte数组
int length = (int)file.length();
byte [] temp = new byte[length];
in.read(temp,0,length);
str = EncodingUtils.getString(temp, "utf-8");
in.close();
}catch(IOException e){
}
return str;
}
7、程序的安装,卸载,更新
//调出系统安装应用
String fileName = Environment.getExternalStorageDirectory() + apkName;
Uri uri = Uri.fromFile(new File(fileName));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
this.startActivity(intent);
//调出系统卸载应用
Uri packageURI = Uri.parse("package: your.app.id");
Intent intent = new Intent(Intent.ACTION_DELETE,packageURI);
startActivity(intent);
8、实现点击两次返回键退出
//第一步,定义一个变量,用于标识是否退出
boolean isExit;
//第二步,重写Activity中onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit();
return false;
} else {
return super.onKeyDown(keyCode, event);
}
}
//第三步,写一个退出方法
public void exit(){
if (!isExit) {
isExit = true;
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
System.exit(0);
}
}
//第四步,根据exit()方法中的的消息,写一个Handler
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
isExit = false;
}
};
Android常用功能代码块的更多相关文章
- Android常用功能代码块(转)
1.设置activity无标题,全屏 // 设置为无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏模式 getWindow(). ...
- IOS开发-OC学习-常用功能代码片段整理
IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...
- day1 函数 (独立功能代码块)
1.引入函数 2.函数执行过程 4.带参数的函数 5.带返回值的函数 6. 多个返回值 (return a,b,c)元组 7.4种函数 1.引入函数 独立功能代码块 ---> 封装 ----&g ...
- 【DRP】-Dao层常用功能代码:增删改查
本系列博客内容为:做DRP系统中Dao层常用功能. 该项目采用MVC架构 C(Controller)控制器,主要职责;1.取得表单参数:2.调用业务逻辑:3.转向页面 M(Model)模型,主要职责: ...
- !!!常用JS代码块 (jquery)
jquery代码块 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> ...
- Android之常用功能代码
透明导航栏 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManage ...
- Android 常用开源代码整理
1.AndroidAnnotations一个强大的android开源注解框架, 基本上可以注入任何类型, 比一般的所谓的注入框架要快, 因为他是通过生成一个子类来实现的绑定.具体查看文档. 2.and ...
- 常用lua代码块
1.读取请求体中参数 local request_method = ngx.var.request_method local args --获取参数的值 if "GET" == r ...
- python功能代码块记录
python Autopep8——按PEP8风格自动排版Python代码(参考链接) autopep8 --in-place --aggressive --aggressive test_autope ...
随机推荐
- hdu 4033 2011成都赛区网络赛 余弦定理+二分 **
二分边长,判断最后内角和是否为2pi,注意l与r的选取,保证能组成三角形 #include<cstdio> #include<iostream> #include<alg ...
- 难得的中文ASP.NET 5/MVC 6入门教程
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于ASP.NET 5还未正式发布,即使是官方文档都还不完善,更不要说系统的中文文档了 ...
- 在ASP.NET 5中读取配置文件
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 在ASP.NET 5中摒弃了之前配置文件的基础结构,引入了一个全新配置文件系统.今天推荐的文 ...
- 主流IM协议简介
四种主流的IM协议, XMPP协议.即时信息和空间协议(IMPP).空间和即时信息协议(PRIM).针对即时通讯和空间平衡扩充的进程开始协议SIP(SIMPLE). XMPP协议: 在这四种协议中,X ...
- 阿里云 OSS+CDN
https://promotion.aliyun.com/ntms/ossedu2.html https://www.aliyun.com/act/aliyun/ossdoc.html 对象存储(Ob ...
- Angular JS 学习之简介
1.Angular JS是一个JavaScript框架,它是一个以JavaScript编写的库,它可以通过<script>标签添加到HTML页面: <script src=" ...
- DSP using MATLAB 示例Example2.12
代码: b = [1]; a = [1, -0.9]; n = [-5:50]; h = impz(b,a,n); set(gcf,'Color','white'); %subplot(2,1,1); ...
- 【第三方登录】之QQ第三方登录
最近公司做了个网站,需要用到第三方登录的东西.有QQ第三方登录,微信第三方登录.先把QQ第三方登录的代码列一下吧. public partial class QQBack : System.Web.U ...
- Transform组件C#游戏开发快速入门
Transform组件C#游戏开发快速入门大学霸 组件(Component)可以看作是一类属性的总称.而属性是指游戏对象上一切可设置.调节的选项,如图2-8所示.本文选自C#游戏开发快速入门大学霸 ...
- [xsd学习]xsd基本要素
一.xsd常用数据格式 xs:string xs:decimal xs:integer xs:boolean xs:date xs:time 二.简易元素 <xs:element name=&q ...