android - 我如何知道用户在应用选择器中点击了哪些应用或按钮?
问题描述
我正在尝试克隆 Uber Driver 应用程序。我得到了这一部分,在收到客户的请求通知后,应用程序将提示应用程序选择器,让用户在用户选择接受客户的请求后选择哪个 3rd 方应用程序(例如谷歌地图或 Waze)导航到客户的位置。
public static final int APP_CHOOSER_LOCATION = 101;
acceptBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String uri = "geo:" + locationInfo.getLatitude() + ","
+locationInfo.getLongitude() + "?q=" + locationInfo.getLatitude()
+ "," + locationInfo.getLongitude();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivityForResult(intent, APP_CHOOSER_LOCATION);
isBack = true;
}
});
所以问题是我怎么知道用户已经完成了他/她的导航?我目前的解决方案是放置一个指标isBack
。默认情况下它将是false
. 当用户点击接受时,isBack
设置为true
。因此,在 onResume 函数中,如果isBack
等于true
,它将继续打开另一个活动。
@Override
public void onResume(){
super.onResume();
if(isBack){
// Proceed to open another actvity
isBack = false;
}
}
但是,如果用户单击取消按钮,这将不起作用。
应用选择器屏幕截图
我尝试使用onActivityResult
,但无论我点击什么应用程序,resultCode
总是显示 0 (或)。RESULT_CANCELED
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "in onActivityResult ");
Log.i(TAG, "resultCode:: "+resultCode);
if (requestCode == APP_CHOOSER_RESPONSE){
if(resultCode == RESULT_OK){
Log.i(TAG, "user select app ");
if(isBack){
// Proceed to open another activity
isBack = false;
}
}
if(resultCode == RESULT_CANCELED){
Log.i(TAG, "is cancelled by user ");
isBack = false;
}
}
}
我的方法错了吗?还是我错过了代码中的任何内容?我被困在这里,我不知道如何继续。任何建议表示赞赏。
解决方案
经过一番挖掘,我在这里找到了这个答案。
这是我更新的意图代码:
acceptBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// always initialise app_name to be empty whenever the button is clicked
Constant.APP_NAME = "";
// a “share” Intent
Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
// a “receiver” Intent
Intent receiver = new Intent(getApplicationContext(), AppSelectorReceiver.class);
//one PendingIntent
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, receiver,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
// Set appChooser Intent
Intent chooser = Intent.createChooser(mapIntent, null, pendingIntent.getIntentSender());
startActivity(chooser);
}
});
在 onResume 函数中,我检查用户是否点击了任何应用程序:
@Override
public void onResume(){
super.onResume();
if(!Constant.APP_NAME.isEmpty()){
Log.i(TAG, "User choose: "+ Constant.APP_NAME);
// Proceed to do something here
}
}
这是我的 AppSelectorReceiver 类:
public class AppSelectorReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
for (String key : Objects.requireNonNull(intent.getExtras()).keySet()) {
try {
ComponentName componentInfo = (ComponentName) intent.getExtras().get(key);
PackageManager packageManager = context.getPackageManager();
assert componentInfo != null;
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(componentInfo.getPackageName(), PackageManager.GET_META_DATA));
Constant.APP_NAME = appName;
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
推荐阅读
- python - 比较网格中的邻居布尔 numpy 数组
- python-3.x - 从可能更改排序的字符串中提取值
- imagemagick - 如何定义 svg-sprite 的宽度/高度并将这些值写入文件(使用 Linux 或 Mac CLI)
- java - 如何在 Neo4j for java 中传递动态生成的属性名称和值列表?
- javascript - 致命错误:CALL_AND_RETRY_LAST 分配失败 - JavaScript 堆内存不足(反应 npm 测试)
- colors - VS Code intellisense 不显示十六进制值颜色的颜色框
- javascript - 为什么我的 messing.getToken() 在 Firefox 中有效,但在 chrome 中无效?
- extjs - IE 中的 JAWS、ExtJS 6 网格
- r - lapply 跨多个数据帧的函数集
- asp.net - HttpPut 的正确语法