This question already has answers here:
How to get the sender of an Intent?
(8 answers)
Closed 3 years ago.
Is there a way to find out, if my Android App was opened by the launcher or by some third party app?
Something like this maybe?
click
I am trying to build an app that can toggle torch. Which already works (I am new to Java and Android). But I want Bixby Button on my Samsung phone to open the app and then toggle torch automatically. Bixby is able to open a app since the newest version.
I found out, that if I open the app from the launcher and resume, it's the same instance. But if Bixby opens the app, it seems to be a new instance every time.
And I don't want to use Availability Service to receive bixby button event.
kind regards
#Vladyslav Matviienko, thank you for this link:
This seems to work:
protected void onResume() {
super.onResume();
Uri uri = this.getReferrer();
if (uri==null) textView.append("Host: null");
else textView.append("Host: " + uri.toString());
}
When opened by launcher, uri is "null", but when opened by bixby, it is "android-app://com.samsung.android.bixby.agent"
EDIT: On a second try launcher is "android-app://com.sec.android.app.launcher".
Related
This question already has answers here:
How to bring an activity to foreground (top of stack)?
(10 answers)
Closed 4 years ago.
I have an app that needs to run 24/7.
I know that android OS kills the process of my app after some time in the background, so i have a Foreground service which is sticky.
Now the thing is, that i want the service to check if my app is running, and if it is not, then i want the service to start the app. Is it possible?
After onStop your activity will be invisible. If you want to see that your activity running, i.e., on the foreground;
public boolean isForeground(String myPackage) {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
return componentInfo.getPackageName().equals(myPackage);
}
If it is not in the foreground, you can use below otherwise;
Intent i = new Intent(this, MyMainActivity.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
Note; another method, you can send message to your service from your activity about its status
or, use idolon's idea]2;
What kind of app is it? I think such types of apps can be very annoying for users.
I am not sure what exactly functionality your app have, but are you sure that you can't achieve that same functionality with using AlarmManger or JobScheduler ?
I am trying to make an android application, with a button I call google navigation and it works correctly, and my question is: somehow I can close the navigation of google once finished and return to my application?
I do not want to press the back button.
This is the method
Uri gmmIntentUri = Uri.parse("google.navigation:q="+ lat + ","+ lng + ");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK&Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
mapIntent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(mapIntent);
If any application can close other applications then there will be a big question about security.
There is no way to close other applications in Android. Also, you can't get a callback once the navigation finished. I have also faced the same issue and resolved it by ChatHead. When the user taps on ChatHead you can redirect the user to your app.
Here is a reference to create ChatHead: https://medium.com/#kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064
I want to play my own custom notification sound to set my app apart from a system default sound. So, when I receive a GCM message, I have a function called generateNotification that puts a notification in the status bar and plays a sound.
Currently, I set the path to the sound like this:
String GENERAL_NOTIFICATION_SOUND = "android.resource://" + MyActivity.getInstance().getPackageName() + "/" + R.raw.sound_file;
This works when the app is open, even when the app is recently closed. But if the app is closed by Android's memory management, then MyActivity.getInstance() returns null, and my app crashes with a NullPointerException without properly displaying a notification because it couldn't resolve the path.
I believe I could just fix this by hardcoding the path, but I feel like there has to be a better way. How can you set a path to files that need to be accessed when the app might not be open?
First, you should not have a MyActivity.getInstance(), as that is a memory leak.
Second, whatever code is raising the Notification already has access to a Context, as that is where you get a NotificationManager from. getPackageName() is a method on Context. So, for example, your GCM IntentService is a Context, and so not only can it use getSystemService() to retrieve a NotificationManager, but it can call getPackageName() on itself to find out your app's package name.
I'm now developing an android application with the NFC concept . In that application I need to pick my application if I swipe the NFC card and if I select my application my application must start calling webservice.
when it comes to my problem, If suppose my app crashed ,when I swipe the card next time ,the option to choose the application doesn't open .Instead,my application directly launched and couldn't call the webservice data.
On the whole.I'm getting last page when it crashed .But I need to open as fresh
I came to know that I need to make changes in OnResume() and OnNewIntent().
I used ,
In OnResume()
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, mNdefExchangeFilters, null);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
NdefMessage[] msgs = getNdefMessages(getIntent());
byte[] payload = msgs[0].getRecords()[0].getPayload();
//Toast.makeText(this, new String(payload), Toast.LENGTH_LONG).show();
Student=new String(payload);
if(Student.equals(rakesh)
{
new Webservice.execute(""); // Calling SOAP Webservice
}
But,I can't find any changes with my problem .and one more thing that the problem will be resolve after I just open and close an another NFC project
Please help.
Yeah ! I got the solution .I forget a simple thing that I left calling onStop() method and my problem was,when my application stops(when Crashed).It runs in background as the previous activity.
I just tried the following,
public void onStop()
{
super.onStop();
this.finish();
}
This may be helpful to others.
Thanks..
Here is my problem. I am developing app that loads some documents from server. I open document in another app via Intent.ACTION_VIEW. This is all working just fine. Problem is that whole app is pin protected so I have to capture events such "sent to background" or "screen lock" to bring up pin screen afterwards and this is not working when another app is opened above mine. So if user opens document then press home button, click on my launch icon from menu then he gets again external app with opened document and with back button access my app again. This is security issue that needs to be fixed.
Here are some code snippets:
Opening document:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivityForResult(intent, 1);
capture sent to background:
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
IN_BACKGROUND = true;
Log.i(PinUtil.class.getSimpleName(), "App sent to background ");
} else {
IN_BACKGROUND = false;
}
}
My question is: Is it possible to detect if my app is sent to background when another app is opened? How not to open another app when my launcher icon is pressed.
Thanks for all responses.
Regards
Lubos
In order to fix this problem:
So if user opens document then press home button, click on my launch
icon from menu then he gets again external app with opened document
and with back button access my app again. This is security issue that
needs to be fixed. Here are some code snippets:
You need to make sure that, when you launch an external app for the user to view a document, that the external app does not run in the same task as your application. It needs to run in a new, separate task. You can do this like this:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Ensure app runs in separate task
Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivity(intent); // Can't use startActivityForResult() here
However, you can't use startActivityForResult() when you launch the external viewer, because an activity running in another task cannot return a result to you. However, most external applications won't return a result when launched with ACTION_VIEW anyway, so it probably isn't a problem.
Then you asked this:
My question is: Is it possible to detect if my app is sent to
background when another app is opened? How not to open another app
when my launcher icon is pressed.
There should be some answers on StackOverflow that can help you determine if your application is in the background (it isn't actually that easy to determine this).
My explanation above should answer your 2nd question. If you don't launch other apps in your task, then only your app will be launched when your launcher icon is pressed.