App updaten via Button

Ich möchte gerne meine App in Zukunft über einen Button updaten, wenn ich eine neue Version dieser App auf meinen FTP server lade.

Das ganze habe ich mit einem Downloadmanager probiert. Hier habe ich jedoch das Problem, dass er die APK vom FTP-Server zwar runterlädt, jedoch nicht am Smartphone speichert. Anscheinend ist sie nach dem download nur temporär da und das Smartphone erkennt sie auch nicht als eine .apk Datei. Das Smartphone schlägt mir verschiedene Apps vor (Chrome, HTML-Anzeige, Word,...) um diese heruntergeladene Datei zu öffnen, jedoch ist es mir nicht möglich die heruntergeladene APK Datei als solche zu Installieren. Vielleicht kann mir jemand helfen, was da genau der Fehler ist bzw. hat jemand eine bessere Lösung um vielleicht so etwas wie einen Check durchzuführen, ob eine neuere Version der APK auf dem FTP-Server vorhanden ist ?



Java:
downloadButton = (Button) findViewById(R.id.btn_update);
        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
            Uri uri = Uri.parse("http://meinftpserver/update.apk");
            DownloadManager.Request request = new DownloadManager.Request(uri);
                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE).setAllowedOverRoaming(true);
                    request.setAllowedOverRoaming(false);

                //show on navigation
                    request.setVisibleInDownloadsUi(true);
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);

                //file open when item on navigation is clicked
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                //name to show while downloading
                request.setTitle("Update");

                //description to show while downloading
                request.setDescription("Downloading " + "Update");

            Long reference = downloadManager.enqueue(request);
            }
        });
 
Vielen Dank für die Antwort. Ich habe den Mime-Type hinzugefügt, es funktioniert nun die manuelle Installation, jedoch habe ich immer noch ein Problem. Ich habe schon einiges ausprobiert, um die Installation zu starten wenn der Download abgeschlossen, da bekomme ich aber einen Parse Error oder die App crasht mit einem Error receiving broadcast Intent.
Java:
downloadButton.setOnClickListener(new View.OnClickListener() {


                                              @Override
                                              public void onClick(View v) {



                                                  dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                                                  String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";

                                                  String fileName = "app_name.apk";
                                                  String url = "http://url/app_name.apk";
                                                  Uri mUri = Uri.parse(url);

                                                  destination += fileName;
                                                  final Uri uri = Uri.parse("file://" + destination);

                                                  File file = new File(destination);
                                                  if (file.exists())
                                                      file.delete();

                                                  DownloadManager.Request request = new DownloadManager.Request(mUri);

                                                  request.setMimeType("application/vnd.android.package-archive");

                                                  request.setAllowedNetworkTypes(android.app.DownloadManager.Request.NETWORK_WIFI | android.app.DownloadManager.Request.NETWORK_MOBILE).setAllowedOverRoaming(true);
                                                  request.setAllowedOverRoaming(false);
                                                  request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, mUri.getLastPathSegment());
                                                  request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);

                                                  dm.enqueue(request);




                                                  final String finalDestination = destination;

                                                  final BroadcastReceiver onComplete = new BroadcastReceiver() {

                                                      public void onReceive(Context ctxt, Intent intent) {
                                                          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                                              Uri contentUri = FileProvider.getUriForFile(ctxt, BuildConfig.APPLICATION_ID + ".provider", new File(finalDestination));
                                                              Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
                                                              openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                                              openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                                              openFileIntent.setData(contentUri);
                                                              startActivity(openFileIntent);
                                                              unregisterReceiver(this);
                                                              finish();
                                                          } else {
                                                              Intent install = new Intent(Intent.ACTION_VIEW);
                                                              install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                                              install.setDataAndType(uri,
                                                                      "application/vnd.android.package-archive");
                                                              startActivity(install);
                                                              unregisterReceiver(this);
                                                              finish();
                                                          }
                                                      }
                                                  };

                                                  registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
                                              }
                                          });
 
Ja, in dem Manifest sind die Permissions für WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE und REQUEST_INSTALL_PACKAGES drinnen.

Im Code habe ich auch wie in deinem Thread die onRequestPermissionsResult, requestPermission und canReadWriteExternal übernommen.

Den Link von dir hatte ich mir schon vor der erstellung des Threads mehrmals durchgelesen und alle Möglichkeiten ausprobiert, leider ohne Erfolg. Entweder ich mache einen Fehler und komme noch nicht drauf oder es gibt ein anderes Problem.
 
Das Problem dürfte am Code des Broadcastreceiver liegen, habe ihn testweise rausgelöscht und der Download funktioniert problemlos ohne dass die App crashed. Aber die Installation eben nur manuell. Sobald ich den Broadcastreceiver im Code habe, crashed die App nach dem Download mit dem "Receiving broadcast Intent Error".
 
Ich habe den Code laut Anleitung erneut aufgebaut und habe den "Receiving Broadcast Intent Error" in den Griff bekommen, der taucht nicht mehr auf.

Das einzige Problem dass ich nun noch habe ist, dass weiterhin ein Parsing Fehler auftritt, sobald er nach Abschluss des Downloads automatisch die .apk Öffnen möchte.

.setMimeType("application/vnd.android.package-archive"); ist festgelegt

.setDataAndType(apkUri, "application/vnd.android.package-archive"); ist festgelegt

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> ist im Manifest


Logcat zeigt keine Fehler an
 

mihe7

Top Contributor
Im Logcat sollte aber schon irgendwas stehen, wenn ein Parsing Fehler auftritt. Kannst Du Dir das mal ohne Filter ansehen?
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
T Android Studio: Einen Button in einer For Schleife verwenden Android & Cross-Platform Mobile Apps 2
W App deinstallieren per Button & Andere App Öffnen per Button Android & Cross-Platform Mobile Apps 24
W Apk automatisch installieren per Button Android & Cross-Platform Mobile Apps 55
W aus Fragmente in andere Klasse wechseln mit Button Android & Cross-Platform Mobile Apps 3
AGW Android Teilen Button ändern Android & Cross-Platform Mobile Apps 14
A Button in SurfaceView integrieren Android & Cross-Platform Mobile Apps 10
J Android App - Browser öffnen und Text eingeben/Button click auslösen Android & Cross-Platform Mobile Apps 10
L Button zum Quadrad zwingen Android & Cross-Platform Mobile Apps 2
S Android Absoluter Neuling: EditText zur Laufzeit verändern bzw. über Button Android & Cross-Platform Mobile Apps 2
L Dialog anzeigen wenn auf Button gedrückt wird. Android & Cross-Platform Mobile Apps 4
S Android ListFragment & ArrayAdapter - Button-Werte werden vergessen Android & Cross-Platform Mobile Apps 0
B Android Abfragen wie lang ein Button gedrückt wurde Android & Cross-Platform Mobile Apps 2
J Android button mithilfe einer Methode automatisch erstellen Android & Cross-Platform Mobile Apps 6
A AlertDialog Enable Button Android & Cross-Platform Mobile Apps 1
J Button array ID Problem Android & Cross-Platform Mobile Apps 2
D Android Activity wechseln per Button Android & Cross-Platform Mobile Apps 3
B Android ringProgressDialog nach Erfolg Button einfärben Android & Cross-Platform Mobile Apps 2
A Android Menü Button oben links Android & Cross-Platform Mobile Apps 1
B Mit Button GeräteEinstellung des Handys öffnen Android & Cross-Platform Mobile Apps 3
M Android Android "Up-Button" extra definieren? Android & Cross-Platform Mobile Apps 1
K Problem mit arraylist und button Android & Cross-Platform Mobile Apps 16
T Button aktivieren per Qullcode... Android & Cross-Platform Mobile Apps 3
T Button geht net... Android & Cross-Platform Mobile Apps 2
D Man sieht nicht ob Button gedrückt wurde! Android & Cross-Platform Mobile Apps 10
K Grafik Tablerow, Button erstreckt sich in der gesamten Breite trotz Beschrenkung durch (max)width Android & Cross-Platform Mobile Apps 2
G Button ein Wert zuweisen Android & Cross-Platform Mobile Apps 5
M 20 Image Button für jede Auflösung positionieren Android & Cross-Platform Mobile Apps 3
B Android Button erstellen nach Vorlage Android & Cross-Platform Mobile Apps 4
L Android Button mit Pfeil nach rechts Android & Cross-Platform Mobile Apps 1
M Einzel Verarbeitung welcher Button angeklickt wurde? Android & Cross-Platform Mobile Apps 6
G Back-Button Methode überschreiben Android & Cross-Platform Mobile Apps 2
P Android Option Button Android & Cross-Platform Mobile Apps 4
G Check Button ist unchecked trotz setChecked(true) Android & Cross-Platform Mobile Apps 6
G Android Button mit Bild Android & Cross-Platform Mobile Apps 4
W Android App Programmierung - Button ganz transparent machen Android & Cross-Platform Mobile Apps 3
L Custom Dialog Button event Android & Cross-Platform Mobile Apps 2
J Button rechtsbündig Android & Cross-Platform Mobile Apps 5
A Android Button Array? Android & Cross-Platform Mobile Apps 6
P Android Button mit bild versehen Android & Cross-Platform Mobile Apps 4
J "Button" auf List erstellen Android & Cross-Platform Mobile Apps 4
U Fire button und Command.BACK, 1 kommen sich in die quere Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben