从我的应用程序中启动一个应用程序,并获得打开第二个应用程序的响应 [英] Launch an application from my application and get responce of opening second app

查看:98
本文介绍了从我的应用程序中启动一个应用程序,并获得打开第二个应用程序的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过这种方法,但没有正确的方法.我正在使用此代码打开另一个应用程序.

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.

推荐答案

尝试使用以下代码检查应用是否正在运行:

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();
    }
}


如果第二个应用程序在您的控制之下,则可以通过startActivityForResult执行操作:


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

在您的SecondActivity中,设置要返回到FirstActivity的数据.如果您不想返回,请不要进行任何设置.

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.

例如:如果要发送回数据,则在secondActivity中:

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();

如果您不想返回数据:

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

现在,在FirstActivity类中,为onActivityResult()方法编写以下代码.

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

这篇关于从我的应用程序中启动一个应用程序,并获得打开第二个应用程序的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆