我如何知道哪个应用程序或按钮用户单击了应用程序选择器?

时间:2018-10-26 10:15:17

标签: android google-maps android-intent waze

我正在尝试克隆Uber Driver应用程序。 我已经讲完了这一部分,在收到客户的请求通知后,该应用程序将提示一个应用程序选择器,供用户选择在用户选择接受客户的请求后,哪个第三方应用程序(例如Google Map或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;
    }

}

但是,如果用户单击取消按钮,则此操作无效。

应用选择器屏幕截图

Image

我尝试使用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;
        }
    }
}

我的方法错误吗?还是我错过了代码中的任何内容? 我被困在这里,我不知道如何进行。任何建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

经过一番挖掘,我在here中找到了这个答案。

这是我更新的意图代码:

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