how to detect if user clicked done or open after application in android has been installed programmatically

后端 未结 2 874
终归单人心
终归单人心 2021-01-16 01:02

I\'m having trouble detecting if the user clicked the dialog, that usually pops up after the android application has been installed. So that I can proceed to the next instal

相关标签:
2条回答
  • 2021-01-16 01:27

    You can't detect that.

    If you are wanting to do some operation on the first run of the application then just store a "first run" flag within your user preferences and default it to true.

    You can then check this on start of your app and perform any necessary operations.

    Some example code for this;

    private boolean prefFirstRun;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        settings = PreferenceManager.getDefaultSharedPreferences(this);
    
        prefFirstRun= settings.getBoolean("FirstRun", true);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
    
        if (prefFirstRun) {
            prefFirstRun = false;
            // Do your initial operations here
        }
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    
        SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("FirstRun", false);
        editor.commit();
    
    }
    
    0 讨论(0)
  • 2021-01-16 01:37

    If i get you correctly,

    • You are trying to programatically install a APK
    • You need to get the status, whether the user pressed buttons Open or Done, after the successful installation.

    This can be done. For this, start the package installer like this.

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(
    Uri.fromFile(new File("Full path to your APK")),
            "application/vnd.android.package-archive"); // the APK path can be in SDCARD or in your application directory. I am sure you know this path.
    
    // start the package-installer activity and wait for result. The second parameter can be used to identify the source of result in `onActivityResult` method. 
    startActivityForResult(intent, 1);
    
    • Now the package installer will be started and your APK will get installed on device.
    • User presses either Open or Done buttons.
    • You gets the program control back in onActivityResult callback function

    Here goes the onActivityResult function:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // requestCode == 1 means the result for package-installer activity
        if (requestCode == 1) 
        {
            // resultCode == RESULT_CANCELED means user pressed `Done` button
            if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "User pressed 'Done' button", Toast.LENGTH_SHORT).show();
            } 
            else if (resultCode == RESULT_OK) {
                // resultCode == RESULT_OK means user pressed `Open` button
                Toast.makeText(this, "User pressed 'Open' button", Toast.LENGTH_SHORT).show();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    
    0 讨论(0)
提交回复
热议问题