ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-06-11 17:09:14  阅读:292  来源: 互联网

标签:插拔 U盘 xhApiManager APK gatsby new import android com


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

    }
}

标签:插拔,U盘,xhApiManager,APK,gatsby,new,import,android,com
来源: https://www.cnblogs.com/crushgirl/p/13094353.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有