Opening phone apps through button - java

I have about 12 buttons that each button opens phone apps by it's name.
Now i can open messages and phone call but for other ones i don't know what should i put.
This is my code for phone call:
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent phonecall = new Intent(Intent.ACTION_VIEW);
phonecall.setData(Uri.parse("tel:"));
startActivity(phonecall);
}
});
"tel:" will open phone call, now i want to know what should i put in "tel:" for other apps, Here's my other buttons need to open:
Camera, Contacts, Browser, File Manager, Settings, Gallery, Clock, Telegram application, Instagram application, Whatsapp application.

This is a part of Implicit Intent.
If you want the Android system to handle the intent for a particular task, you can use this.
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));
startActivity(callIntent);
Example: You can fire an Intent.ACTION_DIAL or Intent.ACTION_CALL without specifying any particular package name to handle this and Android system will handle this intent.
If you want your intent to be handled by a particular application then you can specify the package name of the application.
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
startActivity( launchIntent );

Related

How to open default email client in Android, without sending a message?

I'm looking for a way to open default email application on Android, but without using it to send a message. I know I can do this using mailto:// or intent params, but this automatically opens new message screen. What I want to to archieve, is just opening the app itself.
So far, I have tried
override fun startEmailApplication() {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
But every time I get
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.APP_EMAIL] flg=0x10000000 }
Altrough an email app (AquaMail, Outlook) is installed.
I would use a combinaison a little things.
Detect the responding packages for a given Intent:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities(intent, 0);
boolean canResolve = resolveInfos.size() > 0;
This will list the available packages to respond to such Intent. Using queryIntentActivities() allows me to retrieve ResolveInfo which exposes more information about the app.
Select the first one and open it using its packageName:
if (resolveInfos.size() > 0) {
startActivity(getPackageManager().getLaunchIntentForPackage(resolveInfos.get(0).resolvePackageName))
}
You also won't have the ActivityNotFoundException because we check before hand that something will respond to our Intent. Feel free to handle the failing case in a else.
You can do the following:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
getActivity().startActivity(intent);

Android4.4 can not handle sms intent with "vnd.android-dir/mms-sms"

My application has a button to start default sms activity and
it worked perfectly fine all android version except new one, Android 4.4(kitkat)
Here is the code:
public void onClick(View arg0) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", member.getPhoneNumber().trim());
context.startActivity(smsIntent);
}
And I get error messages
11-08 02:08:32.815: E/AndroidRuntime(14733): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }
I know that google made some changes on how the default sms app handles sms intents.
but my app is not a sms app but it only has function to start default sms app with recipient number. so please help.
To start the SMS app with number populated use action ACTION_SENDTO:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
startActivity(intent);
This will work on Android 4.4. It should also work on earlier versions of Android however as the APIs were never public the behavior might vary. If you didn't have issues with your prior method I would probably just stick to that pre-4.4 and use ACTION_SENDTO for 4.4+.
to start the sms app without insert the number, you should delete setType
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("sms:"));
intent.putExtra("sms_body", "smsMsgVar");
startActivity(intent);
The issue happened when you are testing with Emulator,
Please test with the actual device.
In Kotlin this code works:
val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity)
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.type = "text/plain"
sendIntent.putExtra("address", "sms:"+contactNumber)
sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_msg_body))
Timber.e("defaultSmsPackageName: "+defaultSmsPackageName)
if (defaultSmsPackageName != null){ //Can be null in case that there is no default, then the user would be able to choose any app that support this intent.
sendIntent.setPackage(defaultSmsPackageName)
activity!!.startActivity(sendIntent)
}

Preference: OnClick directing towards social network

I created a Preference Activity where I put various Preference for Social Networks (Facebook, Twitter, Google+), I would like clicking on these preference you may be directed to the respective pages, how can I?
It will open browser with given URL:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
startActivity(browserIntent);
If you want to open app, try this:
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.facebook.katana", "com.facebook.katana.LoginActivity");
startActivity(intent);

populating SMS message in android

Here is the code snippet from our app now.
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sms = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" ));
sms.putExtra("sms_body", text);
startActivity(sms);
}
This works with the stock SMS app, however a tester reported that with handcent SMS they get a blank message. This prompted me to try it with Google Voice, also get a blank message there.
Is there some way I can get my message text to work with all of these other SMS apps?
We have a clipboard functionality, so a poor workaround at least would be having user push clipboard button, then use their messaging app of choice.
Android tries to reuse Intents as much as possible. In some cases when you start an intent, android doesn't create a new Intent and reuse previous intents. To avoid this set this flag of Intent:
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
This flag forces Android to create a new Task with the intent's Activity on top of stack.
When you pass this intent , this will be delivered to all those apps which have registered an activity to receive it. In otherwords you have to use the intent that is registered by the application that u intent to handle your intent

intent to youtube app profile/channel

I am able to intent to the youtube app to view a video easily enough, but how about getting to a profile / channel?
public void YouTube(String id) {
// Play Youtube Video
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+id));
mContext.startActivity(i);
}
I.. just don't know where to really begin here? Is there a specific Uri to parse? I've tried scouring the internet of course and I'm coming up dry for answers. Is it even possible in the first place?
Thanks guys!
By doing the following ,one can launch Youtube App to display channel directly
Intent intent=null;
try {
intent =new Intent(Intent.ACTION_VIEW);
intent.setPackage("com.google.android.youtube");
intent.setData(Uri.parse(url));
startActivity(intent);
} catch (ActivityNotFoundException e) {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
And in order to display channel,keep in mind to give url in format http://www.youtube.com/user/channelName
As of now, there is no specific URI scheme for channels that would trigger the YouTube application directly. The vnd.youtube scheme is defined only for the activity that plays a single video. So, you have to specify the canonical YouTube URL for the channel page, and typically let the user pass through the application chooser dialog - assuming that the device has the YouTube application installed, the dialog would display at least two entries, the second being for the browser.

Categories