android 接u盘不发广播,APK: 接受U盘插拔广播装卸应用

一、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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值