源码下载
https://download.csdn.net/download/qq_31939617/10488907 下载
流程:监听U盘插拔事件,弹出对话框询问是否升级,如果升级则调用升级模块。
MainActivity.class
package com.example.sz.usbupdate;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.File;
import android.net.Uri;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private String path = "/mnt/usbhost1/123.apk";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
udiskPluggedin(); // 监听U盘热插拔模块启动
Log.e(TAG, "------VesionName: " + getVesionName(this));
Log.e(TAG, "-------VesionCode: " + getVersionCode(this));
Log.e(TAG, "---------getPackageName: " + this.getPackageName());
}
/******************************
* 模块1:检测U盘热插拔事件
*
* 使用IntentFilter来选择接收广播的类型
* 注册一个广播接收者来处理接收到广播的事件
*
*****************************/
/* 监听U盘插拔 */
private void udiskPluggedin() {
IntentFilter filter = new IntentFilter();
filter.addAction("android.hardware.usb.action.USB_STATE");
filter.addAction("android.hardware.action.USB_DISCONNECTED");
filter.addAction("android.hardware.action.USB_CONNECTED");
filter.addAction("android.intent.action.UMS_CONNECTED");
filter.addAction("android.intent.action.UMS_DISCONNECTED");
// filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
// filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
// filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
// filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(Intent.ACTION_MEDIA_CHECKING);
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addDataScheme("file");// 系统中每个组件,如果想收取隐式事件(未指定接收者),则必须声明自己的IntentFilter(自我介绍,我对什么样的信件感兴趣)。
UdiskReceiver mReceiver = new UdiskReceiver();
registerReceiver(mReceiver, filter);
}
private class UdiskReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
String action = arg1.getAction();
Log.v(TAG, "action = " + action);
if (action.equals(Intent.ACTION_MEDIA_CHECKING)) {
// Toast.makeText(getApplicationContext(), "正在挂载U盘",
// Toast.LENGTH_SHORT).show();
Log.i(TAG, "正在挂载U盘");
} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
Toast.makeText(getApplicationContext(), "U盘挂载成功",
Toast.LENGTH_SHORT).show();
Log.i(TAG, "U盘挂载成功");
// 显示是否更新对话框
try {
showUpdateDialog();
} catch (NameNotFoundException e) {
e.printStackTrace();
}
/*
* Intent intent = new Intent();
* intent.setClassName("com.serviatech.test",
* "com.serviatech.test.activity.UpdateActivity");
* intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//
* Intent.FLAG_ACTIVITY_CLEAR_TOP | startActivity(intent);
*/
} else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
Toast.makeText(getApplicationContext(), "U盘已移除",
Toast.LENGTH_SHORT).show();
}
}
}
/******************************
* 模块2:获取apk版本信息
*
* AndroidManifest.xml中定义的
* android:versionName 和 VersionCode 信息。 *
*
*****************************/
/*
* 功能:获取apk的VesionName,即AndroidManifest.xml中定义的android:versionName
*/
public String getVesionName(Context context) {
String versionName = null;
try {
versionName = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return versionName;
}
// 功能:获取apk的versionCode,即AndroidManifest.xml中定义的android:versionCode
public int getVersionCode(Context context) {
int versionCode = 0;
try {
versionCode = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return versionCode;
}
/********************
* 模块3:弹出是否更新对话框
*
* *****************/
private void showUpdateDialog() throws NameNotFoundException {
StringBuffer sb = new StringBuffer();
sb.append("当前版本:");
sb.append(getVesionName(this));
sb.append(" VerCode:");
sb.append(getVersionCode(this));
sb.append("\n是否更新?");
Dialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("软件更新")
.setMessage(sb.toString())
// 设置内容
.setPositiveButton("更新", // 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
/**************
* 更新核心代码
*
* ***********/
String fileName = path;
Uri uri = Uri.fromFile(new File(fileName));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,
"application/vnd.android.package-archive");
startActivity(intent);
}
})
.setNegativeButton("暂不更新",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
finish();
}
}).create();// 创建
dialog.show();
}
}
源码下载
https://download.csdn.net/download/qq_31939617/10488907 下载