为什么应用程序无法安装更新apk

时间:2018-09-28 15:31:00

标签: android-studio apk auto-update

无论我能够使用Asynctask从服务器下载更新apk,还是正在使用Firebase来检查当前版本是否比服务器版本和旧版本都要新,我都在构建一个从Play商店私下分发的应用程序。然后将其下载并保存到手机存储中,但是当应用通过打开安装对话框意图安装它时,它暗恋的地方是我的代码(是否有一种特殊的方法来生成具有与原始证书相同证书的更新apk(i只需使用相同的密钥库生成它))

`public class MainActivity extends AppCompatActivity {
long cacheExpiration = 3000;
double version = 0;
int versionCode = BuildConfig.VERSION_CODE;
File downloadfolder;
Context context = this;
File output_file;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    check();}

private void check() {
    final FirebaseRemoteConfig mfirebaseremotecofig = FirebaseRemoteConfig.getInstance();
    mfirebaseremotecofig.setDefaults(R.xml.remote_config_defaults);

    mfirebaseremotecofig.fetch(cacheExpiration).addOnCompleteListener(new OnCompleteListener<Void>() {
       // @SuppressLint("SetTextI18n")
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if(task.isSuccessful()){
                 version = mfirebaseremotecofig.getDouble("version");


                Log.i("Info", "done");
                TextView s = (TextView) findViewById(R.id.textView);
                s.setText("version " + version );
                if(version > versionCode){
                    Log.i("Info", "less");
                    UpdateApp atualizaApp = new UpdateApp();
                    atualizaApp.execute(my server url);
                }
                mfirebaseremotecofig.activateFetched();

            }
            else {
                Log.i("AppConfig","AppConfig  failed to load");
            }
        }
    });
}

class UpdateApp extends AsyncTask<String, Integer, String> {

    ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setTitle("Download in progress...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.setProgress(0);
        progressDialog.show();
        progressDialog.setCancelable(true);
        createFolder();
    }

       @Override
    protected String doInBackground(String... params) {
        String path = params[0];
        int file_length;

        Log.i("Info: path", path);
        try {
            URL url = new URL(path);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            file_length = urlConnection.getContentLength();


            output_file = new File(downloadfolder, "updateapk.apk");
            OutputStream outputStream = new FileOutputStream(output_file);

            InputStream inputStream = new BufferedInputStream(url.openStream(), 8192);
            byte[] data = new byte[1024];
            int total = 0;
            int count;
            while ((count = inputStream.read(data)) != -1) {
                total += count;

                outputStream.write(data, 0, count);
                int progress = 100 * total / file_length;
                publishProgress(progress);

                Log.i("Info", "Progress: " + Integer.toString(progress));
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();


            Log.i("Info", "file_length: " + Integer.toString(file_length));

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Download complete.";

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressDialog.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(String result) {
       toinstall();

        }
    }





private void createFolder() {
    if (isStoragePermissionGranted()) {
        //  String secStore = System.getenv("SECONDARY_STORAGE");
        //  File f_secs = new File(secStore);
       // File folder = new File(Environment.getExternalStorageDirectory()+ File.separator + "DebugData");
        downloadfolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "updateapk");
        if (!downloadfolder.exists()) {
            if (downloadfolder.mkdir()) {
                Log.i("Info", "Folder succesfully created");
            } else {

            }
        } else {
            Log.i("Info", "Folder already exists");
        }

    }
}


public boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    } else { //permission is automatically granted on sdk<23 upon installation
        return true;
    }
}

private void toinstall() {
  //String path = output_file.toString();
  //File toInstall = new File(path, "app-release.apk");
File folderold = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "updateapk");
File output_fileold = new File(folderold, "updateapk.apk");
String pathold = output_fileold.toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

    //Uri apkUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", toInstall);
    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
   // intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/updateapk/" +"updateapk.apk" );
    intent.setData(Uri.parse(pathold));
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
} else {
   // Uri apkUri = Uri.fromFile(new File(pathold));
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse(pathold), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

}
 }}

清单文件

 `<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 package="com.example.youssef"
  >
 <uses-permission 
  android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
  <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WAKE_LOCK" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.intent.extra.NOT_UNKNOWN_SOURCE" />
 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/paths" />
    </provider>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.INSTALL_PACKAGE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="content" />
            <data android:scheme="file" />
            <data android:mimeType="application/vnd.android.package-archive" />
        </intent-filter>

</application>

  </manifest>`

paths.xml文件

 ` <paths xmlns:android="http://schemas.android.com/apk/res/android">
   <files-path name="name" path="."/>
    </paths>`

0 个答案:

没有答案