<MyMarket> <application name="TestApp1" package="com.example.testapp1" versionCode="1" versionName="1.0" url="http://mobile...../android/download/TestApp1.apk"/> ..... </MyMarket>
URL url = new URL(apkurl); HttpURLConnection c = (HttpURLConnection) url.openConnection(); c.setRequestMethod("GET"); c.setDoOutput(true); c.connect(); File file = this.getExternalFilesDir("download"); File outputFile = new File(file, "app.apk"); FileOutputStream fos = new FileOutputStream(outputFile); InputStream is = c.getInputStream(); byte[] buffer = new byte[1024]; int len1 = 0; while ((len1 = is.read(buffer)) != -1) { fos.write(buffer, 0, len1); } fos.close(); is.close(); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive"); startActivity(intent);
private boolean checkNewVersion(String packageName, int versionCodeNew) { List<ApplicationInfo> apps = getPackageManager() .getInstalledApplications(0); for (int i = 0; i < apps.size(); i++) { ApplicationInfo app = apps.get(i); if (packageName.equals(app.packageName)) { PackageManager manager = getPackageManager(); PackageInfo info; try { info = manager.getPackageInfo(app.packageName, 0); int versionCode = info.versionCode; if (versionCodeNew > versionCode) { Toast.makeText(this, "New Version!", Toast.LENGTH_LONG) .show(); return true; } } catch (NameNotFoundException e) { e.printStackTrace(); } } } return false; }
Uri packageURI = Uri.parse("package:"+packageName); Intent intent = new Intent(Intent.ACTION_DELETE, packageURI); startActivity(intent);
public class CustomExceptionHandler implements UncaughtExceptionHandler { private File logsFolder = null; public static final String ERROR_INTENT = "com.example.markettestapp1.SEND_ERROR"; public CustomExceptionHandler(File logsFolder) { this.logsFolder = logsFolder; } @Override public void uncaughtException(Thread thread, Throwable ex) { final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); ex.printStackTrace(printWriter); String stacktrace = result.toString(); printWriter.close(); try { if (!logsFolder.exists()) { logsFolder.createNewFile(); } BufferedWriter writer = new BufferedWriter(new FileWriter(logsFolder, true)); writer.write(""+new Date()+"\n"+stacktrace); writer.close(); } catch (IOException e) { e.printStackTrace(); } Intent intent = new Intent(); intent.setAction(ERROR_INTENT); intent.putExtra("packageName", Test1Application.getApplication().getPackageName()); intent.putExtra("stacktrace", stacktrace); Test1Application.getInstanceApplication().sendBroadcast(intent); android.os.Process.killProcess(android.os.Process.myPid()); } }
public class Test1Application extends Application { @Override public void onCreate() { super.onCreate(); application = this; Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(new File(this.getApplicationContext().getExternalFilesDir(null),"exceptions.log"))); }
public class SendErrorReceiver extends BroadcastReceiver { public static final String ERROR_INTENT = "com.example.markettestapp1.SEND_ERROR"; @Override public void onReceive(Context context, Intent intent) { Intent i = new Intent(context, SendErrorActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.putExtra("stacktrace", intent.getStringExtra("stacktrace")); i.putExtra("packageName", intent.getStringExtra("packageName")); context.startActivity(i); } }
<receiver android:name="com.example.markettestapp1.SendErrorReceiver" android:enabled="true" > <intent-filter> <action android:name="com.example.markettestapp1.SEND_ERROR" > </action> </intent-filter> </receiver>
Source: https://habr.com/ru/post/163397/
All Articles