一、U盘插拔广播 Atom.apk
1.1、AndroidManifext.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gatsby.atom"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!--U盘权限--> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <!--隐藏apk应用图标--> <data android:host="akm.app" android:pathPrefix="/openwith" android:scheme="myapp" /> </intent-filter> </activity> <receiver android:name=".Usbbroadcast"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <!--U盘插拔广播--> <intent-filter> <action android:name="android.intent.action.MEDIA_MOUNTED" /> <action android:name="android.intent.action.MEDIA_UNMOUNTED" /> <action android:name="android.intent.action.MEDIA_REMOVED" /> <action android:name="android.intent.action.MEDIA_EJECT" /> <data android:scheme="file" /> </intent-filter> </receiver> </application> </manifest>
1.2、Usbbroadcast.java
package com.gatsby.atom; import android.content.broadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import com.android.xhapimanager.XHapimanager; import java.io.File; public class Usbbroadcast extends broadcastReceiver { private Context mContext; final static String APK_THOMAS = "mnt/usb_storage/USB_disK"; static String APK_PATH = null; XHapimanager xhapimanager; @Override public void onReceive(Context context, Intent intent) { // Todo Auto-generated method stub this.mContext = context; String action = intent.getAction(); xhapimanager = new XHapimanager(); 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(new PreInstallApk()).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 implements Runnable { @Override public void run() { // 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
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gatsby.test"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".VideoPlayer" android:label="VideoPlayer"> <!-- <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>--> </activity> </application> </manifest>
2.2、styles.xml
<resources> <!-- Base application theme. --> <!-- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">--> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
2.3、colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#6200EE</color> <color name="colorPrimaryDark">#3700B3</color> <color name="colorAccent">#03DAC5</color> <color name="color1">#f00</color> <color name="color2">#06F406</color> <color name="color3">#070707</color> <color name="color4">#FBFBFB</color> <color name="color5">#3F51B5</color> <color name="color6">#0ff</color> <color name="color7">#B7F2ADFB</color> </resources>
2.4、activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
2.5、MainActivity.java
package com.gatsby.test; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import com.android.xhapimanager.XHapimanager; public class MainActivity extends AppCompatActivity { 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 = new Handler(); private Runnable runnable = new Runnable() { @Override public void run() { 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 = new Intent(); intent.setClassName("com.gatsby.test", "com.gatsby.test.VideoPlayer"); startActivity(intent); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); xhapimanager = new XHapimanager(); xhapimanager.XHShowOrHideStatusBar(false); Thread thread = new Thread(runnable); thread.start(); } @Override protected void onDestroy() { super.onDestroy(); xhapimanager.XHShowOrHideStatusBar(true); } }
2.6、activity_video.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="1200dp" android:layout_gravity="center"/> </LinearLayout>
2.7、VideoPlayer.java
package com.gatsby.test; import android.content.Context; import android.os.Bundle; import android.widget.VideoView; import androidx.appcompat.app.AppCompatActivity; import com.android.xhapimanager.XHapimanager; public class VideoPlayer extends AppCompatActivity { VideoView videoView; Context mContext; XHapimanager xhapimanager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video); mContext = getApplicationContext(); videoView = (VideoView) findViewById(R.id.videoView); xhapimanager = new XHapimanager(); xhapimanager.XHShowOrHideStatusBar(false); videoView.setVideoPath("/mnt/usb_storage/USB_disK5/udisk0/Test/Test.mp4"); videoView.start(); }
}