我需要按"上一步"关闭当前的Intent,然后转到我的应用程序



我有一个Unity应用程序,它有Android插件,可以启动安装在我智能手机上的其他应用程序。这里是我的Java类:

public class LaunchOtherApp  extends Activity
{
public static Activity mainActivity;
protected static final String LOGTAG = "MyApp";
private Activity currentActivity;  
private Intent i;

private static final LaunchOtherApp ourInstance = new LaunchOtherApp();
public static LaunchOtherApp getInstance() {
return ourInstance;
}
public LaunchOtherApp(){
Log.i(LOGTAG,"Created LaunchOtherApp");
}
public void Launch( final String pack)
{
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent();
intent.setPackage(pack);
currentActivity = UnityPlayer.currentActivity;
Context context = currentActivity.getApplicationContext();
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));
if (resolveInfos.size() > 0)
{
try
{

ResolveInfo launchable = resolveInfos.get(0);
ActivityInfo activity = launchable.activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
i = new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);

context.startActivity(i);
}
catch (SecurityException e)
{
intent = getPackageManager().getLaunchIntentForPackage(pack);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

startActivity(intent);
}
}
}
});
}
@Override
public void onBackPressed() {
finish();
}

Launch-这是一种当我点击Unity应用程序中的按钮时启动的方法。。。pack变量-它是我的Java方法接收的变量,并在此变量中实例化Application id。例如,我从我的应用程序启动了Youtube,当我在手机上按下Back时,我想关闭Youtube,然后返回到我的Unity应用程序。。。我想我必须使用onBackPressed方法,当我按下Back时开始,但我必须用这个方法写什么
finish();帮不了我((请帮忙……提前谢谢!

实际上,YouTube应用程序是一个单独的应用程序(可能已安装(,您无法修改该应用程序的行为。按下按钮后,会调用Launch方法,并假设YouTube应用程序已启动。意图:

如果使用Intent.FLAG_ACTIVITY_NEW_TASK

YouTube应用程序将在一个比您的新任务中启动,按下返回键将不会返回到您的应用程序。

如果不使用Intent.FLAG_ACTIVITY_NEW_TASK

默认情况下,新意图(此处为YouTube(将在调用活动(您的应用程序/游戏(的任务中启动,调用活动将进入后台。因此,按下返回键将再次返回到您想要的应用程序/游戏。因为按下后退按钮总是从当前任务的背面弹出。

因此,在Launch方法的try块中,删除以下行应该会有所帮助。

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

有关任务和后台堆栈的更多信息,请参阅官方文档。

编辑

我错过了您使用应用程序上下文的机会。但是,您必须从调用活动上下文启动,否则必须使用此标志。所以,我修改了您的Launch方法并将其放在这里,只需将您的Launch方法替换为以下方法:

/* Method to launch an app with its package name */
public void Launch( final String pack)
{
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {

// Using activity context instead of application context
currentActivity = UnityPlayer.currentActivity;
Context context = currentActivity;
PackageManager pm = context.getPackageManager();

// Similar code to launch from package name
Intent intent = pm.getLaunchIntentForPackage(pack);
if (intent == null) {
// The activity cannot be found or the package name cannot be recognized
// Show some message or do something
Toast.makeText(context, "Cannot launch...", Toast.LENGTH_SHORT).show();
} else {
context.startActivity(intent);
}
}
});
}

相关内容

最新更新