手机安全助手---方法

-----------------------------------输入流转字符串
public static String readFromStream(InputStream is) throws IOException {
// 缓存数组
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 一次读取1024个字节
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
// 把输入流写入到缓存数组
baos.write(buffer, 0, len);
}
is.close();
// 把缓存输出转换成String
String result = baos.toString();
baos.close();
return result;
}


-----------------------------------检查是否有新版本,如果有就升级

/**
* 检查是否有新版本,如果有就升级
*/
private void checkUpdate() {
// 联网 需要在子线程中完成 也可以强制在主线程中联网
new Thread() {
public void run() {
// 没有while 会只执行一次 连接请求会阻塞 直到可以链接成功
try {
URL url = new URL(getString(R.string.serverurl));
// 联网
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方式是返回
conn.setRequestMethod("GET");
// 设置超时时间 4秒
conn.setReadTimeout(400);


// 返回响应码
int code = conn.getResponseCode();


// 联网成功
if (code == 200) {
InputStream is = conn.getInputStream();
String result = StreamTools.readFromStream(is);
System.out.println("升级" + result);
} else {


System.out.println("超时" + code);
}


} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("联网失败");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("联网失败IO");
e.printStackTrace();
}
};


}.start();
}
   <string name="serverurl">http://192.168.0.106:8080/updateinfo.html</string>


--------------------------------返回程序版本号

/**
* 返回程序版本号
*/
private String getVersion() {
// 得到管理手机的APK
PackageManager pm = getPackageManager();
try {
// 得到APK清单文件
PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
return info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
return "";
}
}






-----------------------------------升级APK 通过afinal框架进行下载


替换安装要求签名要一致 否则无法替换安装


当包名相同的时候,如果签名相同,替换安装成功。如果签名不相同,安装失败;


当密匙丢失有两种方法安装应用
1.不改变包名 重新签名安装
  不会替换安装也无法正常安装需要把包名相同的apk卸载




2.改变包名重签 
  不会替换安装 但是会安装 变成两个应用


// 查询SD卡是否挂载
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// SD卡挂载
// 通过afinal框架进行下载
FinalHttp finalhttp = new FinalHttp();
finalhttp.download(apkurl,
Environment.getExternalStorageDirectory().getAbsolutePath() + "/mobilesafe2.0.apk",
new AjaxCallBack<File>() {


@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
t.printStackTrace();
Toast.makeText(getApplicationContext(), "下载失败", 0).show();
super.onFailure(t, errorNo, strMsg);
}


@Override
public void onLoading(long count, long current) {
super.onLoading(count, current);
int progrees = (int) (current * 100 / count);
tv_update_progree.setText("下载进度:" + progrees + "%");
}


@Override
public void onSuccess(File t) {
super.onSuccess(t);
// 通过隐示意图安装APK
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.fromFile(t), "application/vnd.android.package-archive");
startActivity(intent);
}


});


} else {
Toast.makeText(getApplicationContext(), "没有SD卡,请安装后重试。", 0).show();
return;
}




-----------------------------------弹出升级对话框

AlertDialog.Builder builder = new Builder(this);
builder.setTitle("升级版本");
// 增加按键
builder.setPositiveButton("立刻升级", new OnClickListener() {


@Override
public void onClick(DialogInterface dialog, int which) {
// 显示进度
tv_update_progree.setVisibility(View.VISIBLE);


// 查询SD卡是否挂载
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// SD卡挂载
// 通过afinal框架进行下载
FinalHttp finalhttp = new FinalHttp();
finalhttp.download(apkurl,
Environment.getExternalStorageDirectory().getAbsolutePath() + "/mobilesafe2.0.apk",
new AjaxCallBack<File>() {


@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
t.printStackTrace();
Toast.makeText(getApplicationContext(), "下载失败", 0).show();
super.onFailure(t, errorNo, strMsg);
}


@Override
public void onLoading(long count, long current) {
super.onLoading(count, current);
int progrees = (int) (current * 100 / count);
tv_update_progree.setText("下载进度:" + progrees + "%");
}


@Override
public void onSuccess(File t) {
super.onSuccess(t);
// 通过隐示意图安装APK
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.fromFile(t), "application/vnd.android.package-archive");
startActivity(intent);
}


});


} else {
Toast.makeText(getApplicationContext(), "没有SD卡,请安装后重试。", 0).show();
return;
}
}


});


builder.setNegativeButton("下次升级", new OnClickListener() {


@Override
public void onClick(DialogInterface dialog, int which) {
// 进入主界面
enterHome();
// 关闭对话框
dialog.dismiss();
}
});
// 不允许点击别的地方
builder.setCancelable(false);
// 显示对话框
builder.show();






------------自定义XML属性---------------
1.在values中新建xml文件 attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <declare-styleable name="SettingItemView">
        <attr name="titles" format="string" />
        <attr name="desc_on" format="string" />
        <attr name="desc_off" format="string" />
    </declare-styleable>


</resources>



2.在XML文件中使用自定义属性

    <com.major.mobilesafe.ui.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        major:desc_off="自动升级已经关闭"
        major:desc_on="自动升级已经开启"
        major:titles="设置是否自动更新" />




3.在JAVA中获得XML中自定义的属性


//注意连接
// 拿到自定义的属性
String titles = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.major.mobilesafe", "titles");
String desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.major.mobilesafe", "desc_on");
String desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.major.mobilesafe", "desc_off");





------------显示设置条---------------


1.布局好XML文件


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


    <TextView
        android:id="@+id/setting_tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dip"
        android:text="设置是否自动更新"
        android:textColor="#000000"
        android:textSize="20sp" />


    <CheckBox
        android:clickable="false"
        android:id="@+id/setting_cb_states"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />


    <TextView
        android:id="@+id/setting_tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/setting_tv_title"
        android:layout_marginLeft="5dip"
        android:text="自动升级已经开启"
        android:textSize="14sp" />


    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:layout_below="@+id/setting_tv_desc"
        android:background="#55000000"
         />


</RelativeLayout>


2.自定义组件 

public class SettingItemView extends RelativeLayout {


private TextView setting_tv_title;
private TextView setting_tv_desc;
private CheckBox setting_cb_states;


private void InitView(Context context) {
View view = View.inflate(context, R.layout.setting_item, this);
setting_tv_title = (TextView) view.findViewById(R.id.setting_tv_title);
setting_tv_desc = (TextView) view.findViewById(R.id.setting_tv_desc);
setting_cb_states = (CheckBox) view.findViewById(R.id.setting_cb_states);
}


public void setDescState(boolean state) {
if (state) {
setDesc("自动升级已经关闭");
} else {
setDesc("自动升级已经开启");
}
}


/**
* 设置标题
* 
* @param text
*/
public void setTitle(String text) {
setting_tv_title.setText(text);
}


/**
* 设置内容
* 
* @param text
*/
public void setDesc(String text) {
setting_tv_desc.setText(text);
}


/**
* 设置是否被选中
* 
* @param checked
*/


public void setCheckBox(boolean checked) {
setting_cb_states.setChecked(checked);
}


/**
* 返回CheckBox状态
* 
* @return
*/


public boolean isCheck() {
return setting_cb_states.isChecked();
}


public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
InitView(context);
}


public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
InitView(context);
}


public SettingItemView(Context context) {
super(context);
InitView(context);
}


}




3.在XML文件中使用
    <com.TenterMobilesafe.ui.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值