怎么安装瑞士军刀??

来源:互联网 发布:java获取网络时间 编辑:程序博客网 时间:2024/06/10 07:52

一、判断Android系统中是否有瑞士军刀的应用。

   第一种方法:  

   Intent intent = new Intent(Intent.ACTION_MAIN);
   intent.addCategory(Intent.CATEGORY_LAUNCHER);
   intent.setComponent(new ComponentName(
     "com.utooo.android.knife.free",
     "com.utooo.android.knife.free.AndroidRuler"));
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
     | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    startActivity(intent);

   如果抛出异常,就说明此系统中没有瑞士军刀的应用。

  第二种方法:

  PackageInfo packageInfo = null;
  try {
packageInfo = this.getPackageManager().getPackageInfo(
"com.utooo.android.knife.free", 0);
  } catch (NameNotFoundException e1) {
e1.printStackTrace();
  }

 如果packageInfo==null,就说明Android系统中没有瑞士军刀的应用。

二、瑞士军刀的apk文件在assets下面,名字为:ruishi_plugin.apk

三、case R.id.ImageButton3:// GPS服务
  {
   // 使用瑞士军刀标志的服务
   Intent intent = new Intent(Intent.ACTION_MAIN);
   intent.addCategory(Intent.CATEGORY_LAUNCHER);
   intent.setComponent(new ComponentName(
     "com.utooo.android.knife.free",
     "com.utooo.android.knife.free.AndroidRuler"));
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
     | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
   try {
    startActivity(intent);
   } catch (Exception e) {
    // new AlertDialog.Builder(Function.this).setTitle("警告!")
    // .setMessage("超级瑞士军刀(版本1.1.1)应用的支持")
    // .setPositiveButton("确定", null).show();
    installApk("ruishi_plugin.apk");

   }

   // Intent intent = new Intent(this, GpsStatusActivity.class);
   // this.startActivity(intent);
  }
   break;

四、

public void installApk(String apkName) {

  FileOutputStream fos;
  InputStream is;
  try {
   is = getAssets().open(apkName);
   fos = openFileOutput(apkName, MODE_WORLD_WRITEABLE);
   byte[] bytes = new byte[512];
   int i = -1;
   while ((i = is.read(bytes)) > 0) {
    fos.write(bytes);
   }

   fos.close();
   is.close();
  } catch (IOException e1) {
   e1.printStackTrace();
  }
  String apkPath = "/data/data/" + this.getPackageName() + "/files";
  File file = new File(apkPath, apkName);
  String permission = "777";
  try {
   String command = "chmod " + permission + " "
     + file.getAbsolutePath();
   Runtime runtime = Runtime.getRuntime();
   runtime.exec(command);
  } catch (IOException e) {
   e.printStackTrace();
  }
  Intent intentInstall = new Intent();
  intentInstall.setAction(android.content.Intent.ACTION_VIEW);
  intentInstall.setDataAndType(Uri.fromFile(file),
    "application/vnd.android.package-archive");
  intentInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intentInstall);
 }

原创粉丝点击