我使用DownloadManager.ACTION_DOWNLOAD_COMPLETE但发生ActivityNotFoundException

时间:2016-04-25 05:55:17

标签: android

我想在我的服务器上获取apk,在android上安装apk后

但是当我安装apk时,它会显示ActivityNotFoundException。

或许,许可问题?

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

感谢 请给我建议

这是一个来源

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Button btn = (Button) findViewById(R.id.btn);
       btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               String url = "myserver/apk";
               File apkFile = new File("/sdcard/Download/openapk.apk");

            Uri mUri = Uri.parse(url);
            DownloadManager.Request r = new DownloadManager.Request(mUri);
            r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "openapk.apk");
            r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            dm.enqueue(r);  //
           }
       });
   }
private BroadcastReceiver completeReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        File apkFile = new File("/sdcard/Download/openapk.apk");
        Uri apkUri = Uri.fromFile(apkFile);
        Intent webIntent = new Intent(Intent.ACTION_VIEW);
        webIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.pacvkage-archive");
        webIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(webIntent);    //ActivityNotFoundException 
    }
};

@Override
public void onResume() {
    super.onResume();
    IntentFilter completeFilter =
            new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    registerReceiver(completeReceiver, completeFilter);
}
}





FATAL EXCEPTION: main
                                                                      Process: company.co.kr.urlupdater, PID: 23341
                                                                      java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=company.co.kr.urlupdater (has extras) } in company.co.kr.urlupdater.MainActivity$2@2184e240
                                                                          at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:769)
                                                                          at android.os.Handler.handleCallback(Handler.java:733)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:136)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                       Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/Download/openapk.apk typ=application/vnd.android.pacvkage-archive flg=0x10000000 }
                                                                          at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
                                                                          at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
                                                                          at android.app.Activity.startActivityForResult(Activity.java:3424)
                                                                          at android.app.Activity.startActivityForResult(Activity.java:3385)
                                                                          at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
                                                                          at android.app.Activity.startActivity(Activity.java:3627)
                                                                          at android.app.Activity.startActivity(Activity.java:3595)
                                                                          at company.co.kr.urlupdater.MainActivity$2.onReceive(MainActivity.java:64)
                                                                          at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:759)
                                                                          at android.os.Handler.handleCallback(Handler.java:733) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                          at android.os.Looper.loop(Looper.java:136) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                          at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:1)

我猜您在将包名设置为Intent时会出现拼写错误。它应该是“application / vnd.android.pa ck age-archive”。所以整行都是:

webIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");

这就是为什么你得到ActivityNotFoundException,因为你提供错误的包裹,并且找不到合适的Activity