My app launches an app to display images selected by the user, but it needs to be able to terminate the displayer app on certain conditions. Here's what I have so far:
Uri attachmentUri = Uri.fromFile(new File("/sdcard/" + filename));
attachmentViewIntent = new Intent(Intent.ACTION_VIEW);
attachmentViewIntent.setDataAndType(attachmentUri, contentType);
attachmentViewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
try {
mContext.startActivity(attachmentViewIntent);
} catch (Exception e) {
}
The images get displayed just fine, but I don't see how to kill the display app. I tried
mContext.stopService(attachmentViewIntent);
but that had no effect (I'm not sure I want to stop the service anyway, just the displayer). Any ideas?
Once started the new activity, it will be up to the user to stop it (or less) and you have no control on how and when it will happen.
Calling Context.stopService(Intent) is used to stop a working Service, hence you have no effect because you started an Activity instead.
EDIT:
If you are not familiar with the concept of task in Android you should absolutely take a look at the docs:
Navigation between apps
Tasks and back stack
Related
I'm making an app in which you can chat and call with other contacts. But in case of calling, I've designed the app in such a way that after typing the number and clicking on the call icon, it takes you to native calls app for calling and updates the call log in my current app.
For this process, this is the code I've written:
if (nativeCall(mobileNumber)) {
Intent intent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("tel:" + mobileNumber));
if (((BaseActivity) context).isNetworkOk()) {
addToUserCallLogs(context, DateUtils.convertTimestampToDate(), contactUri, "Out", System.currentTimeMillis());
}
context.startActivity(intent);
return true;
}
You can see that I'm putting mobile number into the intent and starting it. And I'm using addToUserCallLogs() function to show it in my app's call logs.
This works fine usually, but in the issue is in the following case.
When the user has multiple calling applications(For eg, the user has installed application named SMARTalk. Now he has native caller app and SMARTalk app to call from), in that case the Android system gives options to chose from like this:
Now, if he choses from one of them, even in that case there is no issue. Say he didn't chose any of those and clicked on the other part of the screen. Then this options bar will be closed. Since all this is happening after starting the intent, this call will be added in the call logs of the app from the function addToUserCallLogs(). But I don't want the call to be shown in the call Logs because I haven't done any call.
But according to the code I've written, before starting the intent, I'm adding into my app's call logs database. Is there a way the information of whether the call has happened or not can be sent back from the system to the app?
Or a way to get these options to be shown manually from the app?
Please comment if you need any more explanation.
I guess no way to receive the callback information because ACTION_CALL does not return a result. You can see the output is nothing from docs even you use startActivityForResult
On the first boot itself, I want to set the default browser from the three that are already installed on the system. I do not want to give the user the option to select the default browser, I want to set it for him/her.
How do I go about it?
EDIT : The Phone is running ICS.
It is impossible to do. There is no API method which would allow this.
To do it on first boot make one receiver,whenever boot detects on that time call any activity and on activity onCreate method write this
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity"));
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.BROWSABLE");
Uri uri = Uri.parse(url);
intent.setData(uri);
try
{
startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
This can be done if u develop your own firmware and make it a default app for internet uses.This requires the both firmware and boot permissions from the device to make ur application as the default application.
Any way you can set ur application as always to open the web pages in settings
I have an app 'A'. I am opening another app 'B''s video player and playing a video using an intent URI call like so
String intentURI = "B://this/123";
try {
intent = Intent.parseUri(intentURI, Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
Logger.appendInfoLog("Something went wrong with B", TAG);
e.printStackTrace();
Logger.appendErrorLog(e.getMessage(), TAG);
finish();
}
startActivity(intent);
Now the necessary condition is for that app 'B' to be open in the background for this to work.If the app is closed(killed by Android or manually) or it has crashed,this throws an error.
Is there a way to open that app 'B' first or check its running status and then make the intent URI call. I will not get control back from that app and once I go to the other app I dont have any control on it until the user presses the back button to return to my app.
UPDATE:
I want to start app 'B' first and then call the intent programmatically. Is this possible
UPDATE
I Ran the Catlog app to check what message comes up in app B. It just shows file 123.file (The one i am trying to play in the app B's video player) not found, but when the app is running in the background it goes through fine. It also shows a warning
java.lang.NullPointerException: PrintLn needs a message
and then it says
Activity displayed, but mediaplayer.is not playingVideo
Also the other app is written in flash and packaged as a native app on adobe air
I have an app 'A'. I am opening another app 'B''s video player and playing a video using an intent URI call like so
No, you are not. You can tell that by reading the code -- there is no startActivity() call in that code block.
Now the necessary condition is for that app 'B' to be open in the background for this to work.If the app is closed(killed by Android or manually) or it has crashed,this throws an error.
Then apparently app B has a bug. Please contact the author of app B for assistance.
You can get a list of running apps easily.
ActivityManager manager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
And you can launch apps just as easily.
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package....");
startActivity(LaunchIntent);
You can't, however, launch an app into background, so this might not solve your issue.
You don't need to open that app 'B' first. Just check if that app is running with:
// Get running processes
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
So now you have all the running processes in runningProcesses. Just iterate over the values to find out if your app 'B' is running. An example of this iteration can be found here:
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
}catch(Exception e) {
//Name Not FOund Exception
}
}
Define the intent listener inside of your manifest file.
See the docs for details.
Basically, what you would do is in your AndroidManifest.xml of App B (the app you want to start with the Intent), add a section like:
<receiver android:name=".MyIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Then inside of your MyIntentReceiver class, you would define the code to handle the intent.
public class MyIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
// handle intent here
}
}
I think your problem is that Intent.parseUri() returns an Intent with the action ACTION_VIEW (see http://developer.android.com/reference/android/content/Intent.html#parseUri%28java.lang.String,%20int%29). And from your code, I gather that android does not recognize that it actually has to start activity B for this. ACTION_VIEW is generic, so id does what seems to be most appropriate. If activity B does not fall into that category (probably not the standard video viewing app), it' will not get launched. I would suggest the following:
Find out how to launch activity B (e.g. Intent i = new Intent.("com.packageB.ActivityB"); or similar. Developer should be able to tell you.
Use i.setData() to set the data of your intent to your file.
Use startActivity()
For step 2, there may be other ways Activity B needs the Uri passed. You can get this information from the developer as well.
Using startActivity rids you of having to check whether App B is running. Something that Android does anyway.
I think it is possible to start another apk from your own, just check this answer. The problem is that if you dont know their uri, it will be hard to do what you propose.
Now you can either ask the developers for the proper names or take a look at your own risk (i don't know about its legality).
I have an activity and using an intent I call a certain portion of another app. That is lets say my app A has an activity 1 from which I call activity 1 of app B which I have no control over using the following mechanism
Activity A
Intent intent = null;
try {
intent = Intent.parseUri("URI://sample/100/1000",
Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException e) {
--ERROR
}
startActivity(intent);
When I press the back button it returns to Activity 1 of my app A. Since I lose control I am not able to figure how I can capture the amount of time the user spent on the Activity from app B. Is there some way I can capture this? I know if the user goes to the home screen from that app my data will be skewed but I am ok with that. But using system time in seconds can I capture the time?
May be you can use "startActivityForResult". This way, you can write "onActivityResult" and check how much time has elapsed since you started the intent.
More about it can be found here: How to manage `startActivityForResult` on Android?
I am developing an app were i need to use some sort of method to handel two different intents from a click of a Button. The first is only sometimes able to start without crashing the app. Therfore i need to start another intent were the app normally would have crashed.
Better explained do I need some sort of method that launches another intent, if the 1. intent can not start a new activity, then start the 2 intent.
I really appreciate some sort of formula, instead of a link or reference.
You are looking for a try catch block.
try
{
startActivity(intent);
}
catch(Exception e)
{
startActivity(another_intent);
}
Try/Catch in your code. You can then launch another intent if the first one fails.
If your application is crashing, you should sort this out. You wont be able to start another intent if the application crashes though, because the Applications Process has been killed by the OS. You will need to detect if you can launch the activity, otherwise launch another.
Fix the crash, then implement some switching logic based on what used to be causing a crash.