1.根据xutils3的文档说明,创建了一个类对xutils进行初始化
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(false);
}
}
2.然后在配置文件中做了如下配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.gao.mysafe">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES"
tools:ignore="ProtectedPermissions" />
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/safe_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
</application>
</manifest>
3.在activity中调用了xutils的下载方法
//apk下载地址,放置apk的路径
//1.获取sd卡路径
String path = Environment.getExternalStorageDirectory().getPath() + "/mysafe.apk";
//2.发送请求,获取apk,并放到指定路径
RequestParams rp = new RequestParams(url);
rp.setSaveFilePath(path);
rp.setAutoRename(true);
x.http().get(rp, new Callback.ProgressCallback<File>() {
//下载成功
@Override
public void onSuccess(File result) {
ToastUtils.show(SplashActivity.this, "下载成功,开始安装!!");
installAPK(result);
}
//下载出现问题
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public void onWaiting() {
}
//刚刚开始下载
@Override
public void onStarted() {
}
//下载过程中方法
@Override
public void onLoading(long total, long current, boolean isDownloading) {
}
});
然后在模拟器中出现了如下问题
D/dalvikvm: Not late-enabling CheckJNI (already on)
I/InstantRun: Instant Run Runtime started. Android package is com.gao.mysafe, real application class is com.gao.mysafe.MyApp.
W/InstantRun: No instant run dex files added to classpath
D/dalvikvm: GC_FOR_ALLOC freed 68K, 5% free 2929K/3056K, paused 2ms, total 3ms
I/dalvikvm-heap: Grow heap (frag case) to 34.554MB for 33177612-byte allocation
D/dalvikvm: GC_FOR_ALLOC freed <1K, 1% free 35328K/35460K, paused 5ms, total 5ms
D/OpenGLRenderer: Enabling debug mode 0
I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
W/dalvikvm: VFY: unable to resolve interface method 16550: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
W/dalvikvm: VFY: unable to resolve interface method 16552: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
W/dalvikvm: VFY: unable to resolve interface method 16556: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
W/dalvikvm: VFY: unable to resolve virtual method 747: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
W/dalvikvm: VFY: unable to resolve virtual method 769: Landroid/content/res/TypedArray;.getType (I)I
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
W/dalvikvm: VFY: unable to resolve virtual method 710: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
W/dalvikvm: VFY: unable to resolve virtual method 712: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
W/dalvikvm: VFY: unable to resolve instanceof 158 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList
W/dalvikvm: VFY: unable to resolve virtual method 470: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
4.调用安装方法后也出现了错误
private void installAPK(File file) {
//系统应用界面,源码,安装apk入口
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory("android.intent.category.DEFAULT");
//文件作为数据源
intent.setData(Uri.fromFile(file));
//设置安装类型
intent.setType("application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
/*
也可以用下面代码一行完成
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
*/
try{
startActivityForResult(intent, 0);
}catch (Exception e){
e.printStackTrace();
}
}
问题如下:
D/dalvikvm: GC_FOR_ALLOC freed 422K, 2% free 36957K/37436K, paused 3ms, total 3ms
W/System.err: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.DEFAULT] typ=application/vnd.android.package-archive flg=0x10000000 }
W/System.err: at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
W/System.err: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
W/System.err: at android.app.Activity.startActivityForResult(Activity.java:3424)
W/System.err: at android.app.Activity.startActivityForResult(Activity.java:3385)
W/System.err: at com.gao.mysafe.SplashActivity.installAPK(SplashActivity.java:338)
W/System.err: at com.gao.mysafe.SplashActivity.access$500(SplashActivity.java:30)
W/System.err: at com.gao.mysafe.SplashActivity$6.onSuccess(SplashActivity.java:280)
W/System.err: at com.gao.mysafe.SplashActivity$6.onSuccess(SplashActivity.java:275)
W/System.err: at org.xutils.http.HttpTask.onSuccess(HttpTask.java:447)
W/System.err: at org.xutils.common.task.TaskProxy$InternalHandler.handleMessage(TaskProxy.java:198)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:136)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
弄了半天不知道是什么问题,在真机上也不好使,安装不上,求大神解答一下,谢谢了!!