Launch an application from my application and get

2020-05-07 19:12发布

问题:

I try with this ways but did not get correct way to do this.I am using this code to open another app.

Now i want to know when ever i close or open my second app then i want to get some result of closing second app or opening second app in to my first app.Can it is possible or not if it is possible to get any responce of opening second app or closing second app.

Intent intentt = new Intent(Intent.ACTION_MAIN);
intentt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentt.setComponent(new ComponentName("com.example.abc","com.example.abc.MainActivity"));
startActivity(intentt);

This code open my second app after press back button my second app will close at the time of close and opening second app i want to get some result at the time of second app opening or closing some result,i also follow these links also.

I also follow this link to explore or find solution

how i can do this...Thanks in Advance.

with this code my second app is open but after closing second app how do i find or handle callback of second app.

回答1:

You Should use startActivityForResult() method to open or close second app because here we pass the intent and request code as parameter which helps to get response in onActivityResult() method.



回答2:

Try With this code for opening one application to another application

Intent launchIntent = 
getPackageManager().getLaunchIntentForPackage("com.package.address");

if (launchIntent != null) { 
startActivity(launchIntent);//null pointer check in case package name 
 was not found
}


回答3:

From your FirstApp call the SecondApp using startActivityForResult() method

For example:

  intent = new Intent(Intent.ACTION_VIEW);
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  intent.setData(Uri.parse("market://details?id=" + "com.package.name"));

    startActivityForResult(intent, 2404);

When user press back button it will notify in onActivityResult method of FirstApp.

Pass data with Intent and then finish that activity in Second App.Put whatever your data is with putExtra. Write this in Second App:

byte[] byteArray = stream.toByteArray();
            Intent intent = new Intent();
            intent.putExtra("tile", 1);
            intent.putExtra("image", byteArray);
            setResult(2404,intent);
            finish();

Write following code for the in FirstApp. To access the result passed from Second App

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 2404) {
        if (data != null)// To check for NullPointerException { 
            byte[] byteArray = data.getByteArrayExtra("image");
            Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
          //Your code 
        }
    }
}

For more Detail check this link:

  1. StartActivity 2.StartActivityForResult 3.OnActivityResult


回答4:

Try this code for checking whether app is running or not:

ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++)
{
    if(procInfos.get(i).processName.equals("com.android.maven")) 
    {
        Toast.makeText(getApplicationContext(), "YourApp is running", Toast.LENGTH_LONG).show();
    }
}

If the 2nd App is under your control then you can do by startActivityForResult:

In your SecondActivity set the data which you want to return back to FirstActivity. If you don't want to return back, don't set any.

For example: In secondActivity if you want to send back data:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result); // this response code will be caught by the 1st activity 
setResult(Activity.RESULT_OK,returnIntent); 
finish();

If you don't want to return data:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();

Now in your FirstActivity class write following code for the onActivityResult() method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}//onActivityResult