Tuesday, March 12, 2019

Android开发笔记-ch3.4.16 Schedule, Timer/Alarm, Delay, Handler, 3.4.17 Install APK programmatically


3.4.16 Schedule, Timer/Alarm, Delay, Handler

Refer to this SO, which talks about Timer, AlarmManager, ScheduledThreadPoolExecutor. A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required. Successive executions of a periodic task scheduled via scheduleAtFixedRate(Runnable, long, long, TimeUnit) or scheduleWithFixedDelay(Runnable, long, long, TimeUnit) do not overlap. Sample with 10min interval as below:
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate (new Runnable() {
         public void run() {
            // call service
         }
      }, 0, 10, TimeUnit.MINUTES);
Alarm (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.
For postpond an action, can also send a message with delay to a handler to process, like below code:
StartActivity.this.handler.sendEmptyMessageDelayed(StartActivity.STARTLOAD_TV_UPDATE, 5000);
For a task with counting down, use CountDownTimer as mentioned in 3.4.10.
For handler, if postDelay is used, to cancel all callback, refer this SO: handler.removeCallbacksAndMessages(null);

3.4.17 Install APK programmatically

Refer to this SO and this SO. You can easily launch a market link or an install prompt:
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse("file:///path/to/your.apk"), "application/vnd.android.package-archive");
startActivity(promptInstall);
Intent goToMarket = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("market://details?id=com.package.name"));
startActivity(goToMarket);
For uninstall APK, refer to this SO and this SO:
Uri packageURI = Uri.parse("package:"+"your.packagename.here");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
API level 14 introduced two new actions: ACTION_INSTALL_PACKAGE and ACTION_UNINSTALL_PACKAGE. Those actions allow you to pass EXTRA_RETURN_RESULT boolean extra to get an (un)installation result notification. Example code:
String app_pkg_name = "com.example.app";
int UNINSTALL_REQUEST_CODE = 1;
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);  
intent.setData(Uri.parse("package:" + app_pkg_name));  
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, UNINSTALL_REQUEST_CODE);
And receive the notification in your Activity#onActivityResult method:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == UNINSTALL_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Log.d("TAG", "onActivityResult: user accepted the (un)install");
        } else if (resultCode == RESULT_CANCELED) {
            Log.d("TAG", "onActivityResult: user canceled the (un)install");
        } else if (resultCode == RESULT_FIRST_USER) {
            Log.d("TAG", "onActivityResult: failed to (un)install");
        }
    }
}

0 Comments:

Post a Comment