android如何安静地安装apk?

时间:2015-05-20 09:01:31

标签: android permissions install

我试图制作一个安静地安装apk的应用。 但是当我尝试时,它总会出错。 我真的不知道我现在应该做什么。 我想我已经尝试过在网上发布的所有方式。任何人都可以帮助我了解我现在应该尝试的其他事情吗?

package com.example.filetest;

import java.io.File;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewDebug.FlagToString;
import android.widget.Toast;

public class MainActivity extends Activity {

    int count = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        /* File file = new File("/storage/external_storage/sda1","Scratch-v0.953.apk"); 
         Intent intent = new Intent(Intent.ACTION_VIEW);     intent.setDataAndType(Uri.fromFile(file),
     "application/vnd.android.package-archive");
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(intent);
    */
    File file = new File("/storage/external_storage/sda1/FingerPCSettings");
    //File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    Log.i("TAG","exists = "+file.exists());
    File[] temp = file.listFiles();
    if(temp==null){
        Log.e("TAG","temp is null");
        return;
    }
        InstallAPK(temp[0]);




}

public static void InstallAPK(File file){
    Log.i("TAG", "install called "+file);

    if(file.exists()){
        try {   
            String command;
            command = "/system/bin/pm install -r " + file.getAbsolutePath();
            Log.i("TAG", "command="+command);
            command = command.substring(0, 1);
            Process proc = Runtime.getRuntime().exec(new String[] { command });
            proc.waitFor();

        } catch (Exception e) {
            e.printStackTrace();
        }
     }
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
protected void onResume() {
    super.onResume();
    Log.i("TAG", "###########RESUME CALLEd");
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

当我尝试使用此代码时,我会收到如下错误。

05-20 17:34:18.898: W/System.err(6272): java.io.IOException: Error running exec(). Command: [/] Working Directory: null Environment: null
05-20 17:34:18.898: W/System.err(6272):     at java.lang.ProcessManager.exec(ProcessManager.java:211)
05-20 17:34:18.898: W/System.err(6272):     at java.lang.Runtime.exec(Runtime.java:173)
05-20 17:34:18.898: W/System.err(6272):     at java.lang.Runtime.exec(Runtime.java:128)
05-20 17:34:18.898: W/System.err(6272):     at com.example.filetest.MainActivity.InstallAPK(MainActivity.java:59)
05-20 17:34:18.898: W/System.err(6272):     at com.example.filetest.MainActivity.onCreate(MainActivity.java:43)
05-20 17:34:18.898: W/System.err(6272):     at android.app.Activity.performCreate(Activity.java:5273)
05-20 17:34:18.898: W/System.err(6272):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
05-20 17:34:18.898: W/System.err(6272):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-20 17:34:18.898: W/System.err(6272):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-20 17:34:18.898: W/System.err(6272):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-20 17:34:18.898: W/System.err(6272):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-20 17:34:18.898: W/System.err(6272):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-20 17:34:18.898: W/System.err(6272):     at android.os.Looper.loop(Looper.java:136)
05-20 17:34:18.898: W/System.err(6272):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-20 17:34:18.908: W/System.err(6272):     at java.lang.reflect.Method.invokeNative(Native Method)
05-20 17:34:18.908: W/System.err(6272):     at java.lang.reflect.Method.invoke(Method.java:515)
05-20 17:34:18.908: W/System.err(6272):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
05-20 17:34:18.908: W/System.err(6272):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
05-20 17:34:18.908: W/System.err(6272):     at dalvik.system.NativeStart.main(Native Method)
05-20 17:34:18.908: W/System.err(6272): Caused by: java.io.IOException: Permission denied
05-20 17:34:18.908: W/System.err(6272):     at java.lang.ProcessManager.exec(Native Method)
05-20 17:34:18.908: W/System.err(6272):     at java.lang.ProcessManager.exec(ProcessManager.java:209)
05-20 17:34:18.908: W/System.err(6272):     ... 18 more

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.filetest"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.system"
coreApp = "true"
   >

<uses-sdk
    android:minSdkVersion="19"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.DELETE_PACKAGES"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 

    >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

我现在无法得到我应该做的事情。有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

尝试此操作来运行命令:

Process proc = Runtime.getRuntime().exec(new String[] { command });

而不是使用代码:

 Process p = Runtime.getRuntime().exec("cd " + Environment.getExternalStorageDirectory() + "/.command");

并验证你的命令构造字符串应该是正确的。在你的日志中打印子字符串。

答案 1 :(得分:0)

抛出此异常是因为您尝试在根目录中创建该文件。

在创建新文件时删除开头的/,以便您的路径成为相对路径:

File file = new File("storage/external_storage/sda1/FingerPCSettings");