• APK: 接受U盘插拔广播装卸应用


    一、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();
    
        }
    }
  • 相关阅读:
    js图片轮换
    PHP如何打造一个高可用高性能的网站呢?
    php中浮点数计算问题
    jQuery ajax()使用serialize()提交form数据
    js最新手机号码、电话号码正则表达式
    swoole是如何实现任务定时自动化调度的?
    Facebook的“零售吸引力”,互联网营销 狼人:
    Google勇敢新世界,互联网营销 狼人:
    Facebook的成功之道,互联网营销 狼人:
    李彦宏分享百度危机中如何“弯道超车”,互联网营销 狼人:
  • 原文地址:https://www.cnblogs.com/crushgirl/p/13094353.html
Copyright © 2020-2023  润新知