安卓 如何最小化通话屏幕并推进我自己的任务 - Android 10 (2.3.3)



感觉这个问题已经被问了,但没有一个符合我的需求,也没有一个解决方案对我有用。

我有一个应用程序,其中包含一个扩展到PhoneStateListener的类来侦听以下状态:

  • CALL_STATE_RINGING(这里我用传入的电话号码设置一个全局字符串变量)
  • CALL_STATE_OFFHOOK(在这里,我想将我的任务/流程移到前面,以便用户在屏幕上看到我的应用程序,而不是呼叫中屏幕)

我需要一种方法来最小化通话中屏幕(进入通知区域) 我知道可以做到(有证据),但我不知道该怎么做 最小化后,我应该能够将我的任务移动到前面,而之前移动到后面。

我的应用程序启动,用户登录并按住后退按钮,用户保持登录状态,但应用程序将移至后台。如果发生来电,我想查看我的应用程序中是否存在呼叫者电话号码(应用程序保留几个用户信息),然后在呼叫屏幕最小化后转到用户信息。

当我在CALL_STATE_OFFHOOK状态下运行此代码时,调用最小化,地图全屏加载(校样)

public String IncomingTelephoneNumber;
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
IncomingTelephoneNumber = incomingNumber;
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
activity.startActivity(intent);
break;
}
}
}
}

无法获取地图源代码。 以同样的方式我想这样做,但是对于我自己的应用程序,我尝试了这个,但没有工作,通话屏幕仍然在应用程序前面(或者根本没有调用应用程序)。代码没有中断或任何东西,只是我的应用程序没有出现。

public String IncomingTelephoneNumber;
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
IncomingTelephoneNumber = incomingNumber;
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
try {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(270532608);//This is the flag which is used from the Launcher, LogCat helped here //flg=0x10200000
intent.setClassName(activity.getPackageName(), Splash.class.getCanonicalName());
activity.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
}

我已经尝试了其他方法将应用程序带回前台,但不起作用。

我看到了这篇文章,但没有解决方案。

我使用安卓版本10,因为它是正在使用的设备所需要的(三星P-1000平板电脑,安卓2.3.3是最新的正式版本)

任何帮助将不胜感激。它可能是火车砸碎或需要调用的非常简单的东西。但我希望这样做,就像地图一样。

编辑:我遇到了一个最小化通话屏幕的解决方案(不是最好的解决方案,但它有效)

public String IncomingTelephoneNumber;
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
IncomingTelephoneNumber = incomingNumber;
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
try {
//This will simulate pressing the Home button
//Thus Minimizing the In Call Screen
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(startMain);
//This is the part still not working Correctly
//Moving my Task/Process to the Front for the user to see
//This is Android 10, I still can not find a solution to move task to front
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(activity, Splash.class.getCanonicalName());
activity.startActivity(intent);
} catch (Exception e) {
Log.v("", "e: " + e.getMessage());
e.printStackTrace();
}
break;
}
}
}
}

我找到了我的特定问题的解决方案, 在玩了许多不同的设置和阅读论坛之后,这就是我想出的,并在Android GingerBread 2.3.3(版本10)上工作

为了尽量减少通话并转到主屏幕,我已经完成了此操作

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(startMain);

我决定不使用上述内容,只使用以下基本上可以满足我需求的(解决方案适用于两部分,意图代码和 Android 清单文件中的代码)

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Here the activity class in Question "JobDetail"
intent.setClassName(activity.getPackageName(), JobDetail.class.getCanonicalName());
//These extras I add and then use them in my
//JobDetail class to show the specific details for the caller
intent.putExtra("JobId", JobId);
intent.putExtra("FromCall", "true");
activity.startActivity(intent);

清单部分

<activity android:name="JobDetail" android:configChanges="orientation|keyboardHidden"
android:taskAffinity="" android:launchMode="singleInstance"
android:alwaysRetainTaskState="true" />

在清单文件中的特定活动类下,您要将其置于前面,在我的情况下,在InCallScreen上显示它,添加以下三个属性/标记

  • android:taskAffinity="(我不确定这是否有什么作用,但我把它留在那里)
  • android:launchMode="singleInstance">(我认为这将启动活动的全新实例,但只有此选项和上面的意图,我发现它会重新启动整个应用程序,因此添加了最后一个属性/标签)
  • android:alwaysRetainTaskState="true">(这可确保它直接打开活动类,而不是重置整个应用程序)

在此之后,处理您的后退键,以防您要启动的活动类位于另一个父活动等应用程序中的深处。

还发现使用moveTaskToBack(true);来最小化应用程序在姜饼中效果不佳。我不知道这背后的所有技术细节,但这告诉我,有问题的应用程序被直接移动到堆栈的后面,因此发现将您的任务放在前面并不是一件容易的事。而是使用以下意图调用(模拟按下主页按钮,从用户light-hide应用程序)。

@Override
public void onBackPressed() {
//moveTaskToBack(true);
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
//Show toast to notify the user that your application is still running in the background
return;
}

我希望这可以帮助有人也遇到与我相同的旧硬件和旧软件问题。

最新更新