一、U盘插拔广播 Atom.apk
1.1、AndroidManifext.xml
1.2、UsbBroadcast.java
packagecom.gatsby.atom;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.util.Log;importcom.android.xhapimanager.XHApiManager;importjava.io.File;public class UsbBroadcast extendsBroadcastReceiver {privateContext mContext;final static String APK_THOMAS = "mnt/usb_storage/USB_DISK";static String APK_PATH = null;
XHApiManager xhApiManager;
@Overridepublic voidonReceive(Context context, Intent intent) {//TODO Auto-generated method stub
this.mContext =context;
String action=intent.getAction();
xhApiManager= newXHApiManager();if(action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
String path=intent.getData().getPath();
Log.d("gatsby", "path = " + path);//这里是U盘路径
if(path.contains(APK_THOMAS)) {
Log.d("gatsby", "Receiver:ACTION_MEDIA_MOUNTED->TestAPK");
APK_PATH=path;new Thread(newPreInstallApk()).start();
}
}else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED) ||action.equals(Intent.ACTION_MEDIA_EJECT)) {
Log.d("gatsby", "onReceive: Usb is remove!");
xhApiManager.XHUninstallOnBackground("com.gatsby.test");
}
}class PreInstallApk implementsRunnable {
@Overridepublic voidrun() {//TODO Auto-generated method stub
File file = new File(APK_PATH + "/udisk0/Test/Test.apk");
String preinstallPath=file.getPath();
Log.d("gatsby", "file.getPath()->" +preinstallPath);if(file.exists()) {
Log.d("gatsby", "thomas will be start install TestAPK");
xhApiManager.XHInstallOnBackground(preinstallPath,"com.gatsby.test");
}else{
Log.d("gatsby", "file not exists");
}
}
}
}
二、测试APK Test.apk
1.1、功能:a、轮循七种颜色 b、播放视屏
2.1、AndroidManifes.xml
2.2、styles.xml
@color/colorPrimary
@color/colorPrimaryDark
@color/colorAccent
2.3、colors.xml
#6200EE
#3700B3
#03DAC5
#f00
#06F406
#070707
#FBFBFB
#3F51B5
#0ff
#B7F2ADFB
2.4、activity_main.xml
2.5、MainActivity.java
packagecom.gatsby.test;importandroid.content.Intent;importandroid.os.Bundle;importandroid.os.Handler;importandroid.util.Log;importandroid.widget.ImageView;importandroidx.appcompat.app.AppCompatActivity;importcom.android.xhapimanager.XHApiManager;public class MainActivity extendsAppCompatActivity {
ImageView imageView;int count = 0;
XHApiManager xhApiManager;final int[] colors = new int[]{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
R.color.color7,
};
Handler handler= newHandler();private Runnable runnable = newRunnable() {
@Overridepublic voidrun() {if (count < 7) {
Log.d("gatsby", "Thread.currentThread().getId()->" +Thread.currentThread().getId());
imageView.setBackgroundResource(colors[count]);
count++;
handler.postDelayed(runnable,20000);
}else{try{
Thread.sleep(1000);
}catch(Exception e) {
e.printStackTrace();
}
Log.d("gatsby", "count->" +count);
Intent intent= newIntent();
intent.setClassName("com.gatsby.test","com.gatsby.test.VideoPlayer");
startActivity(intent);
}
}
};
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView) findViewById(R.id.imageView);
xhApiManager= newXHApiManager();
xhApiManager.XHShowOrHideStatusBar(false);
Thread thread= newThread(runnable);
thread.start();
}
@Overrideprotected voidonDestroy() {super.onDestroy();
xhApiManager.XHShowOrHideStatusBar(true);
}
}
2.6、activity_video.xml
2.7、VideoPlayer.java
packagecom.gatsby.test;importandroid.content.Context;importandroid.os.Bundle;importandroid.widget.VideoView;importandroidx.appcompat.app.AppCompatActivity;importcom.android.xhapimanager.XHApiManager;public class VideoPlayer extendsAppCompatActivity {
VideoView videoView;
Context mContext;
XHApiManager xhApiManager;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
mContext=getApplicationContext();
videoView=(VideoView) findViewById(R.id.videoView);
xhApiManager= newXHApiManager();
xhApiManager.XHShowOrHideStatusBar(false);
videoView.setVideoPath("/mnt/usb_storage/USB_DISK5/udisk0/Test/Test.mp4");
videoView.start();
}
}