How to not show app chooser Android - android

I have an intent that dials a number as follows:
Intent intent = new Intent(isTelephonyEnabled() ? Intent.ACTION_CALL : Intent.ACTION_VIEW);
intent.setData(Uri.parse(number));
cordova.getActivity().startActivity(intent);
callbackContext.success();
The function isTelephoneEnabled() just makes sure that a Telephony Manager exists:
private boolean isTelephonyEnabled(){
TelephonyManager tm = (TelephonyManager)cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
return tm != null && tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
}
The issue is, an app chooser opens up and asks for instance if I want to dial using Skype or through the normal Phone app. How do I prevent the app chooser from opening up? I want it to go straight to the default option everytime (the normal phone).

I just give you an idea to do what you want.
You can get the list of all the activities that may receive your intent. Use PackageManager.queryIntentActivities for it. This method returns a list of ResolveInfo objects. And you may retrieve an Activity name out of ResolveInfo
Then you should choose one activity, that you want to open, and send your intent to this activity (use Intent.setComponent for instance).

Related

Dynamic Shortcuts to open Fragments in Android

Hi I'm developing an application that has 2 types of user: Admin and normal user; The admin obviously has more actions than the normal user, so I need to use dynamic Shortcuts, depending on the user the Shortcuts are created.
At this point everything is already codified and working. However, at the time of assigning an Intent, even to the MainActivity I receive an error.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.motusk.Monit", "com.motusk.Monit.activity.MainActivity"));
shortcut.add(new ShortcutInfo.Builder(this, "ub")
.setShortLabel("Location")
.setLongLabel("Location")
.setIcon(Icon.createWithResource(this, R.drawable.bg))
.setIntent(intent)
.build());
I also tried with:
Intent intent = new Intent(this, MainActivity.class);
But, in both I get the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.motusk.Monit/com.motusk.Monit.activity.MainActivity}: java.lang.NullPointerException: intent's action must be set
In what I need help with is:
1) How to create a correct Intent to the MainActivity?
2) How to know which shortcut was pressed? (The process of depending on which shortcut was pressed to open a certain Fragment will be performed in the MainActivity, that's why I need to know which Shortcut was pressed).
As per the error message, your Intent must set an action with setAction:
intent.setAction("LOCATION_SHORTCUT");
You can then check the action in your Activity:
String action = getIntent() != null ? getIntent().getAction() : null;
if ("LOCATION_SHORTCUT".equals(action)) {
// Show the correct fragment
}

getExtras() return null on Application level (-class)

On my Application level I receive null for getExtras(), but on Activity level i can see them correctly.
public class MyApplication extends Application
{
#Override
public void onCreate() {
super.onCreate();
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.MyApp");
if (intent != null){
String mStaticWorldUrl = intent.getStringExtra("arg1Name");
String mStaticWorldIconUrl = intent.getStringExtra("arg2Name");
Log.i("LOG", mStaticWorldUrl + " --- " + mStaticWorldIconUrl);
}
}
}
I'm calling the app from some shortcuts that were created by this code:
(- each shortcut has different Extras sent to the Intent)
// create a shortcut for the specific app
public static void createShortcutForPackage(Context context,
String packageName, String className, String shortcutName,
String arg1Name, String arg1Val, String arg2Name, String arg2Val,
int iconID) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, className));
PackageManager pm = context.getPackageManager();
Context pkgContext = createPackageContext(context, packageName);
if (pkgContext == null)
return;
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutIntent = pm.getLaunchIntentForPackage(packageName);
if (arg1Name != null)
shortcutIntent.putExtra(arg1Name, arg1Val);
if (arg2Name != null)
shortcutIntent.putExtra(arg2Name, arg2Val);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, iconID));
shortcut.putExtra("duplicate", false);
context.sendBroadcast(shortcut);
}
how can I read these Extras on the Application level?
or is there any other way to create different shortcuts for application and read its' "parameters" data on Application?
The Application class is static for the application: there is only ever a single instance of it for your app's process. If your app has been launched with a normal launch Intent, rather than a shortcut you created, then no extras would be present. The app process does not die when HOME or BACK is pressed, so the Intent used to launch the package may not be what you think it should be.
You should not need to look at the Intent at the Application level. Intent objects are not intended to be "sent" there, but rather to an Activity, Service or BroadcastReceiver.
This is conceptual error which facing to get data in application class using getExtra which is the method of Intent
Reason for this issue:
No extras is because the queried intent was one that was generated
one line earlier, it is the intent that the OS has generated for the
package as requested in the following code: Intent intent =
getPackageManager().getLaunchIntentForPackage("com.example.M‌​yApp");
Intent objects within an Application class instance: they are not
delivered there
Let's understand following things to use in upcoming usage while anyone want to get data in application level
What is Intent ?
What is the Use of Intent?
What other things can we use to achieve this?
What is Intent?
An Intent provides a facility for performing Late runtime binding between the code in different applications . Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
extras - This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.
What is the Use of Intent?
Use to intents facilitate communication between components in several ways, Followings are standard use
To start an activity.
To start a service.
To deliver a broadcast
What other things can we use to achieve this?
There are lots of things we can use to achive this and solved this
issue.
But right now here i mentioned only one which is standard and secured
to use application
Content provider: To offer a file from your app to another app is to
send the receiving app the file's content URI and grant temporary
access permissions to that URI. Content URIs with temporary URI
access permissions are secure because they apply only to the app that
receives the URI, and they expire automatically.

Android- How can I choose an app for a future intent, without executing that intent? [duplicate]

This question already has answers here:
Android. Custom Intent chooser
(3 answers)
Closed 6 years ago.
I would like to be able to have the ability for users to be able to set up a predefined email, which is stored and then sent later based upon some trigger. The stumbling block here is that I want the user to be able to choose what app they use for this. Simply calling Intent.createChooser does nothing on its own, the app is not selected until startActivity() is called on it, which then opens the selected app. The problem here is, since I want to send the email later, either the app chooser is not triggered until the email is (which may be while the screen is off), or the app chooser is triggered at setup-time, which will then leave my app, interrupting setup, when an app is selected in the chooser.
Is there some way to have a createChooser-style menu open, allowing the user to select one of the apps available for association with a certain intent, but then merely return the name of that app to my app, so that its information can be stored for creating and executing later intents?
You can do this:
PackageManager packageManager = getPackageManager();
List<String> results = new ArrayList<String>();
Intent intent = new Intent(YourAction); //Put any action string you have in mind
//Or any Intent intent = new Intent(...) you think fits
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : activities) {
ActivityInfo activityInfo = resolveInfo.activityInfo;
if (activityInfo != null) {
results.add(activityInfo.name);
}
}
results will hold the activity names that can perform the action you need.

Android - Open an external app and then call an intent URI

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).

When is a Home not a Home (Launcher related)

I am running into a very strange situation whereby I am getting a list of all the installed packages that have a CATEGORY_HOME intent.
My intention is to manually launch the native Home application (which is currently NOT the Default native Home application, because my application has that role).
So, the method I'm using (beneath) correctly identifies that there are two apps that are set up as CATEGORY_HOME.
When I try to launch the one that is mine (fetching the Launcher activity) it works fine. However, when I try to fetch the Launcher activity for the default one, it comes back as null.
So... I'm stumped. How do I determine what I should actually be launching when the package name of the stock home app returns null when I try and pull the relevant Launch activity via getLaunchIntentForPackag from it?
Here's what I'm doing (with some comments to avoid confusion), and for the record I know that not all of them will have the expression "android" in the name space, but I'm trying to get this to work initially on a device that does come back with that string so that part of it isn't the issue.
//get a list of all apps that set themselves up as CATEGORY_HOME
final Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
final List<ResolveInfo> list = ((PackageManager)getPackageManager()).queryIntentActivities(intent, 0);
String packageName = null;
//look for the one that has the word android in the package name
for(ResolveInfo ri : list){
if(ri.activityInfo.packageName.indexOf("android") != -1)
//this does get set correctly and looks like "com.sec.android.app.launcher"
packageName = ri.activityInfo.packageName;
}
PackageManager pm = SlidePlayer.this
.getPackageManager();
Intent it = pm.getLaunchIntentForPackage(packageName);
//it is NULL so this doesn't work
startActivity(it);
***EDIT
Trying out the following methodology based on CommonsWare's advice...
String packageName = null;
String className = null;
for(ResolveInfo ri : list){
//L.d("HOME PACK = " + ri.);
if(ri.activityInfo.packageName.indexOf("android") != -1){
className = ri.activityInfo.applicationInfo.className;
packageName = ri.activityInfo.applicationInfo.packageName;
}
}
//PackageManager pm = SlidePlayer.this
//.getPackageManager();
Intent it = new Intent();//pm.getLaunchIntentForPackage(packageName);
//both packageName and className appear to be set correctly
//packageName = "com.sec.android.app.launcher"
//className = "com.android.launcher2.LauncherApplication"
it.setClassName(packageName, className);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
EDIT 2*
semi huzzah...
On one device (Samsung Note II) the key is to set it via Component like so...
Intent it = new Intent();//pm.getLaunchIntentForPackage(packageName);
ComponentName cn = new ComponentName(packageName, className);
it.setComponent(cn);
//it.setClassName(packageName, className);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
This doesn't work on the Samsung Tab 10" or the Motorola M (which are the only other 2 devices I have thus far tested with this methodology).
getLaucnIntentForPackage looks specifically for CATEGORY_INFO or CATEGORY_LAUNCHER - not CATEGORY_HOME, in which case it will return null. From the Documentation:
public abstract Intent getLaunchIntentForPackage (String packageName)
Added in API level 3 Return a "good" intent to launch a front-door
activity in a package, for use for example to implement an "open"
button when browsing through packages. The current implementation will
look first for a main activity in the category CATEGORY_INFO, next for
a main activity in the category CATEGORY_LAUNCHER, or return null if
neither are found.
However, when I try to fetch the Launcher activity for the default one, it comes back as null.
That's because most firmware home screens are not designed to be launched by home screens, and so probably do not include a launcher activity.
How do I determine what I should actually be launching when the package name of the stock home app returns null when I try and pull the relevant Launch activity via getLaunchIntentForPackag from it?
Since it is a home screen, you know that it must have an activity with the same ACTION_MAIN/CATEGORY_HOME as you do. Find that one and start it.

Categories

Resources