有点意思,今天面试一个游戏公司,游戏盒子的功能

你好,我们是做游戏盒产品的,直接说需求吧?类以的产品:摸摸鱼游戏盒  233乐园   这些都是游戏应用市场,里面的游戏APK下载安装完后不在手机桌面显示ICO图标,只在游戏盒里面有入口, 你看一下这种技术你能实现吗?  

杠了一下  花了我2个小时吧   原谅我是菜鸡

package com.example.boxedtest;

import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ImageView img;
    private TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView(){
        img = findViewById(R.id.img);
        txt = findViewById(R.id.txt);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = txt.getText().toString();
                // 映射一个hashmap
                // 或服务器端mysql中设计一张让游戏游戏名称和报名对应起来,
                // 通过游戏名称可以获取报名;
                String gamePackageName = getGamePackageName(str);//获取游戏对应的包名
                if(isApplicationInstalled(gamePackageName)){ //判断游戏是否安装
                    startGame(gamePackageName);//启动游戏
                }else{
                    downLoadAndInstall();// 如果没有安装有则下载安装后启动
                }
            }
        });
    }
    private String getGamePackageName(String str){
        //通过网络获取包名
        return "";
    }

    //通过packageManager判断app是否暗转
    private boolean isApplicationInstalled(String packageName){
        PackageManager pm = getPackageManager();
        boolean installed = false;
        try{
            pm.getPackageInfo(packageName,PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch(PackageManager.NameNotFoundException e){
            installed = false;
        }
        return installed;
    }

    //启动游戏app的首页面
    private void startGame(String packageName){
        try{
            PackageManager pm = getPackageManager();
            PackageInfo pi = getPackageManager().getPackageInfo(packageName, 0);
            Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
            resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            resolveIntent.setPackage(pi.packageName);

            List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);

            ResolveInfo ri = apps.iterator().next();
            if (ri != null ) {
                packageName = ri.activityInfo.packageName;
                String className = ri.activityInfo.name;

                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);

                ComponentName cn = new ComponentName(packageName, className);

                intent.setComponent(cn);
                startActivity(intent);
            }
        }catch (PackageManager.NameNotFoundException e){

        }
    }
    //下载安装app  app为无icon的app通过intent启动
    private void downLoadAndInstall(){
        //1). 主线程, 显示提示视图: ProgressDialog
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();

        //准备用于保存APK文件的File对象 : /storage/sdcard/Android/package_name/files/xxx.apk
        File apkFile = new File(getExternalFilesDir(null), "update.apk");

        //2). 启动分线程, 请求下载APK文件, 下载过程中显示下载进度
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    //1. 得到连接对象
                    String path = "http://192.168.10.165:8080/Web_Server/L04_DataStorage.apk";
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //2. 设置
                    //connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(10000);
                    //3. 连接
                    connection.connect();
                    //4. 请求并得到响应码200
                    int responseCode = connection.getResponseCode();
                    if(responseCode==200) {
                        //设置dialog的最大进度
                        dialog.setMax(connection.getContentLength());


                        //5. 得到包含APK文件数据的InputStream
                        InputStream is = connection.getInputStream();
                        //6. 创建指向apkFile的FileOutputStream
                        FileOutputStream fos = new FileOutputStream(apkFile);
                        //7. 边读边写
                        byte[] buffer = new byte[1024];
                        int len = -1;
                        while((len=is.read(buffer))!=-1) {
                            fos.write(buffer, 0, len);
                            //8. 显示下载进度
                            dialog.incrementProgressBy(len);

                            //休息一会(模拟网速慢)
                            //Thread.sleep(50);
                            SystemClock.sleep(50);
                        }

                        fos.close();
                        is.close();
                    }
                    //9. 下载完成, 关闭, 进入3)
                    connection.disconnect();

                    //3). 主线程, 移除dialog, 启动安装
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                            Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE");
                            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                            startActivity(intent);
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值