判斷服務是否開啟,應用是否安裝,並安裝應用

huidaoli發表於2014-08-31
//檢查應用程式是否安裝並安裝應用程式

        public boolean checkApkExist(Context context, String packageName) {
                if (packageName == null || "".equals(packageName))
                        return false;
                try {
                        ApplicationInfo info = context.getPackageManager()
                                        .getApplicationInfo(packageName,
                                                        PackageManager.GET_UNINSTALLED_PACKAGES);
                        return true;
                } catch (NameNotFoundException e) {
                        return false;
                }
        }

        private void installVoiceServiceApk() {

                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(Intent.ACTION_VIEW);
                String type = "application/vnd.android.package-archive";
                AssetManager assets = ProActivity.this.getAssets();
                try {
                        //當檔案比較大的時候不能用這個方法 來讀取Stream ss.read(buffer) = -1  我的apk大小為5M
                        InputStream ss = assets.open(AsrService.apk");
                        //使用下面這個方法 沒問題
                        InputStream is = getClass().getResourceAsStream(
                                        "/assets/AsrService.apk");

                        FileOutputStream fos = ProActivity.this.openFileOutput(
                                        "AsrService.apk", Context.MODE_PRIVATE
                                                        + Context.MODE_WORLD_READABLE);
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while ((len = is.read(buffer)) != -1) {
                                fos.write(buffer, 0, len);
                        }
                        fos.flush();
                        is.close();
                        fos.close();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                File f = new File(ProActivity.this.getFilesDir().getPath()
                                + "/AsrService.apk");

                // String path = "file:///android_asset/ZXing.apk";
                // File f = new File(path);
                intent.setDataAndType(Uri.fromFile(f), type);
                ProActivity.this.startActivity(intent);

        }
//檢查服務是否啟動
        private boolean isStartService(Context ctx) {
                ActivityManager mActivityManager = (ActivityManager) ctx
                                .getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RunningServiceInfo> currentService = mActivityManager
                                .getRunningServices(100);
                final String igrsClassName = "com.iflytek.asr.AsrService"; //serviceName
                boolean b = igrsBaseServiceIsStart(currentService, igrsClassName);
                return b;
        }

        private boolean igrsBaseServiceIsStart(
                        List<ActivityManager.RunningServiceInfo> mServiceList,
                        String className) {
                for (int i = 0; i < mServiceList.size(); i++) {
                        if (className.equals(mServiceList.get(i).service.getClassName())) {
                                return true;
                        }
                }
                return false;
        }

 

相關文章