简体   繁体   English

Android如何静默安装apk?

[英]android how to install apk silently?

I'm trying to make an app that installs apk silently. 我正在尝试制作一个可静默安装apk的应用程序。 But when I try, it always gives errors. 但是当我尝试时,总是会出错。 I dont really get what I should do now. 我没有真正得到我现在应该做的。 I think I've already tried all the ways posted on the web. 我想我已经尝试了所有发布在网络上的方法。 Can anyone help me know anything else I should try now? 谁能帮助我知道我现在应该尝试的其他事项吗?

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);
}
}

When I try this code, I get errors as follows. 当我尝试此代码时,出现以下错误。

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

Here's my manifest 这是我的清单

<?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>

I can't get what I should do right now.. Does Anyone know how to fix it? 我现在不知道该怎么办。有人知道如何解决此问题吗?

Try this to run your command: 尝试执行以下命令:

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

Instead of this use the code: 代替此使用代码:

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

And also verify your command constructed string should be correct.print the substring in your log. 并验证您的命令构造的字符串应该正确。在日志中打印子字符串。

This exception is thrown because you are trying to create the file in the root directory. 引发此异常是因为您试图在根目录中创建文件。

Remove the / at the beginning when creating the new file so your path becomes a relative path: 创建新文件时,请在开头删除/ ,这样您的路径就成为相对路径:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM