I am trying to make a clone of Uber Driver app. I got this part covered, where upon receiving a client's request notification, the app will prompt an App Chooser for user choose what 3rd party app (e.g. Google Map or Waze) to navigate to the client's location after the user chose to accept client's request.
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;
}
});
So the problem is how do I know user has finish with his/her navigation?
My current solution is putting an indicator isBack
. By default it will be false
. When user has click accept, isBack
is set to true
.
So, in the onResume function, if isBack
is equal to true
, it will proceed to open another activity.
@Override
public void onResume(){
super.onResume();
if(isBack){
// Proceed to open another actvity
isBack = false;
}
}
However, this does not work if the user click the Cancel button.
App Chooser Screenshot
I tried using onActivityResult
but the resultCode
always shows 0 (or RESULT_CANCELED
) no matter what app I clicked.
@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;
}
}
}
Is my approach wrong? Or did I miss anything in my code? I am stuck here and I do not know how to proceed. Any advice is appreciated.
After some digging, I found this answer in here.
This is my updated intent code:
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);
}
});
in the onResume function, I check whether the user has clicked any application:
@Override
public void onResume(){
super.onResume();
if(!Constant.APP_NAME.isEmpty()){
Log.i(TAG, "User choose: "+ Constant.APP_NAME);
// Proceed to do something here
}
}
And this is my AppSelectorReceiver class:
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();
}
}
}
}