【问题标题】:android how to install apk silently?android如何静默安装apk?
【发布时间】:2015-05-20 09:01:30
【问题描述】:

我正在尝试制作一个静默安装 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>

我现在无法得到我应该做的事情..有人知道如何解决它吗?

【问题讨论】:

  • 我需要你的帮助。你能给我一些时间吗?我会非常感谢你

标签: android permissions installation


【解决方案1】:

试试这个来运行你的命令:

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

使用代码代替这个:

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

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

【讨论】:

  • 谢谢,但是.. 嗯.. 我不明白.. 这个代码是干什么用的?
  • 使用 API 来使用 exec 运行你的命令可能会有所帮助。
  • 仍然无法正常工作.. 将我的命令变量放入您的 .command 部分是否正确?
  • 这是运行命令的一种方法(无论您尝试在 adb shell 终端执行什么),因此在命令变量中分配/保存您预期的命令
【解决方案2】:

抛出这个异常是因为你试图在根目录中创建文件。

在创建新文件时删除开头的/,这样你的路径就变成了相对路径:

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

【讨论】:

  • 谢谢,但我已经在使用“子字符串”来取消第一个斜线,所以我认为这不是问题。
  • 我刚试过你的,不幸的是它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2019-10-10
  • 2011-01-04
相关资源
最近更新 更多