码迷,mamicode.com
首页 > 移动开发 > 详细

Android小结(各种系统信息,界面等获取方法)

时间:2015-09-18 15:20:08      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

 

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小结(各种系统信息,界面等获取方法)

标签:

原文地址:http://www.cnblogs.com/seven-sky/p/4819070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!