How to restart an application completely?

前端 未结 7 1481
长发绾君心
长发绾君心 2021-01-04 17:35

I have an application which starts a Remote Service in its first launched activity. Then, in another activity, the user can set the configuration of the application. Please

相关标签:
7条回答
  • 2021-01-04 18:11

    try this code,

    public void onClick(DialogInterface dialog, int which) {
        sauvegarde();
        restart(2);
    }
    

    restart method

    public void restart(int delay) {
        Intent launchIntent = new Intent(context, YourStartUpActivity.class);
        PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, launchIntent , 0);
        AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        manager.set(AlarmManager.RTC, System.currentTimeMillis() + delay, intent);
        System.exit(2);
    }
    
    0 讨论(0)
  • 2021-01-04 18:17

    try this

       public void reload() {
         Intent intent = getIntent();
         overridePendingTransition(0, 0);
         intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
         finish();
    
         overridePendingTransition(0, 0);
         startActivity(intent);
        }
    
    0 讨论(0)
  • 2021-01-04 18:19

    You can use the Androids system AlarmManager like this:

    Code to restart the app in your activity:

    AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(this.getBaseContext(), 0, new    Intent(getIntent()), getIntent().getFlags()));
    System.exit(2);
    

    An example can be looked up here

    UPDATE

    As @CommonsWare pointed out, its a bad way to design your app, when you have to restart it (bad practice). If you really want to do it, you can try setting alarmmanager to start your app in a second after you killed your own process:

    AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(this.getBaseContext(), 0, new    Intent(getIntent()), getIntent().getFlags()));
    android.os.Process.killProcess(android.os.Process.myPid());
    
    0 讨论(0)
  • 2021-01-04 18:27

    Solution of AlarmManager can not work because on kill app all pending intents can be removed. On android-10 I have this issue.

    But I found other way - create Instrumentation class and call startInstrumentation on Context. Worked very nice.

    boolean android.content.Context.startInstrumentation(ComponentName className, String profileFile, Bundle arguments)

    Start executing an android.app.Instrumentation class. The given Instrumentation component will be run by killing its target application (if currently running), starting the target process, instantiating the instrumentation component, and then letting it drive the application.

    This function is not synchronous -- it returns as soon as the instrumentation has started and while it is running.

    Instrumentation is normally only allowed to run against a package that is either unsigned or signed with a signature that the the instrumentation package is also signed with (ensuring the target trusts the instrumentation).

    Instrumentation must be defined in manifest. Google how use it.

    0 讨论(0)
  • 2021-01-04 18:28

    By Using a Pending Intent You can Restart your Application when you want,Just use the below Lines of code

    int pendingId_Intent = 987654;
    Intent YourMainActivityIntent = new Intent(Context, YourMainActivity.class);
    
    PendingIntent intentPending = PendingIntent.getActivity(context, 
    pendingId_Intent,    YourMainActivityIntent, 
    PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarm_manager = 
    (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarm_manager.set(AlarmManager.RTC, System.currentTimeMillis() + 100, 
    intentPending);
    System.exit(0);
    
    0 讨论(0)
  • 2021-01-04 18:35

    This is the newest answer with the latest update 2017 tested and working like a charm hope that help :)

         Intent i = getBaseContext().
                   getPackageManager().
              getLaunchIntentForPackage(getBaseContext().getPackageName());
               i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//clear the stack
               i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               finish();//don`t forget to finish before starting again
    
                            startActivity(i);//start the activity again
    
    0 讨论(0)
提交回复
热议问题