How to open android default apps activity in setting via Intent? - java

How to open the default handler page in the android setting via intent?
or How we can open directly the default SMS page?
it seems from android Q to upper we can not Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT

Found the answer, you can open this page by this code:
//This code will work only on android version N and above
//Add if condition to prevent crash on older devices
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent intent = new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (requestCode != -1)
act.startActivityForResult(intent, requestCode);
else
act.startActivity(intent);
}

Another way with this code to open an intent default app,
in this case, I try to set the default launcher app
// dialog set default app
#RequiresApi(Build.VERSION_CODES.Q)
private fun showLauncherSelection() {
val roleManager = this.getSystemService(Context.ROLE_SERVICE)
as RoleManager
if (roleManager.isRoleAvailable(RoleManager.ROLE_HOME) &&
!roleManager.isRoleHeld(RoleManager.ROLE_HOME)
) {
val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_HOME)
startActivityForResult(intent,0)
}
}
You can set the other Default app with change the ROLE_HOME following this Role Manager. If you want to change SMS default app, try to change the ROLE_HOME to ROLE_SMS

Related

resolveActivity works if I don't check if it is null

I wrote this code to open a website in the browser from my app in android studio(google):
String google = "http://www.google.com";
Uri webAddress = Uri.parse(google);
Intent goToGoogle= new Intent(Intent.ACTION_VIEW, webAddress);
if(goToGoogle.resolveActivity(getPackageManager()) != null) {
startActivity(goToGoogle);
}
the app just does what I want it to do when I do not put the if statement, otherwise the button does nothing. Why is that?
thanks
The resolveActivity() method returns the Activity Component that is used to handle the Intent, so, if there is an Activity handling the intent, it will return true
Make sure that an Activity is handling your intent, placing this code into a java class of an Activity.
String google = "http://www.google.com";
Uri webAddress = Uri.parse(google);
Intent goToGoogle= new Intent(Intent.ACTION_VIEW);
goToGoogle.setData(webAdress);
if(goToGoogle.resolveActivity(getPackageManager()) != null) {
startActivity(goToGoogle);
}

Programmatically choose the phone's original default dialer instead of any other options

When I try to make a call from my app using
intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode));
startActivity(intent);
I get the option
I want to programmatically choose the dialer that the user will use to make the call. I want the user to automatically use Phone (the original dialer) instead of Skype or any other option.
From googling, I found this option below but it only allows the developer to make the user choose the developer's own app as a default dialer. I want to programmatically ask the user to choose the original phone's default dialer "Phone" so that the user is no longer asked the question.
From google, I found this link: Programmatically change the "Use by default for this action"
which has this option:
Intent intent = new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER)
.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, getPackageName());
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_CODE_SET_DEFAULT_DIALER);
} else {
Log.w(getLocalClassName(), "No Intent available to handle action");
}
But what I want is a bit different.
Use the below code:
Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
it is working for me
Try This
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "mobilePhone"));
context.startActivity(intent);

Is it possible to use createChooser but not actually starting the intent (yet)?

In my app I have a share button which allows the user to share some text through other apps such as Messenger. I use createChooser to let the user select the app they want to use. The tricky part is, after they've selected the app, I don't want to start the selected app immediately, but I need to do some communication with the server first (to get the exact text to share), and I also want to prompt some message on the screen at this stage, and then I can start the share intent of the selected app.
In other words, the chooser is simply an interface for selection, I don't want it to actually start the intent; I will start it manually a little later.
I tried to modify the intents that are collected into the chooser in such a way that they simply redirect back to my own app. However, I have problem retaining their icons and label. No matter what I do, some of the selections always change their icons and labels to those of my own app.
Here's roughly what my current code looks like:
PackageManager pm = activity.getPackageManager();
Intent sendIntent = new Intent()
.setAction(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, text)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setType("text/plain");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, PackageManager.MATCH_DEFAULT_ONLY);
List<LabeledIntent> intentList = new ArrayList<>();
for (ResolveInfo ri : resInfo) {
String packageName = ri.activityInfo.packageName;
String name = ri.activityInfo.name;
if (name.contains("facebook.messenger")
|| name.contains("naver.line")
|| name.contains("android.mms")
|| name.contains("clipboard")
) {
ComponentName cn = new ComponentName(packageName, name);
Intent intent = new Intent(sendIntent)
.setPackage(packageName)
.setComponent(cn); // I tried to do somthing here but it doesn't work
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
// loadLabel and icon doesn't always get the correct thing
}
}
Intent first = intentList.remove(0);
Intent[] extra = intentList.toArray(new Intent[0]);
Intent receiver = new Intent(activity, ShareReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(first, title, pendingIntent.getIntentSender());
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extra);
activity.startActivity(chooser);
"but I need to do some communication with the server first"
Do the communication earlier, store and use it when required?
"the chooser is simply an interface for selection, , I don't want it
to actually start the intent; I will start it manually a little
later."
Have you heard of ModalBottomSheet reference to implement the custom chooser for yourself as required.
"I tried to modify the intents that are collected into the chooser in
such a way that they simply redirect back to my own app."
Use startActivityForIntent in that case; when you intent an app, close and return back to your app, handle it onActivityResult whatever you want to do.
" However, I have problem retaining their icons and label."
Are you referring to the icons and labels of other apps in the chooser dialog?

How to make Notification Silent using RingtoneManager

I have added a checkbox preference in preferences.xml. There is a settings.java file which has defined all the preferences.
Now There is a seperate java file which displays a notification when a message comes in.
If the checkbox is true, I would like to make the notification silent.
if(PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_SILENT_MODE, "").equals("true"))
//Make the notification silent ie.
else
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = alarmSound;
There is a TYPE_NOTIFICATION method associated with Ringtone manager for notification type of sound. Is there a way to make this silent?
I know there is a way to make it silent using AudioManager. setRingerMode(AudioManager.RINGER_MODE_SILENT);
But here, ringtone manager is being used.
Edit--FIGURED IT OUT
(flag is a boolean value which returned the checkbox preference result from SharedPreferences)
settings.java file-
public boolean checksilentmode(Context mContext)
{
SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(mContext);
return prefs.getBoolean(PREF_SILENT_MODE,DEFAULT_SILENT_MODE);
}
Another java file-
`if(flag) //if flag is true ie silent mode is enabled.
{
alarmSound=null;
}
else
{
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
notification.sound = alarmSound;`
No need to do all of this ...if you are showing notification using NotificationCompact.Builder do something like below
notificatiomCompactBuilder.setDefaults(4);

Can you open multiple files with intent in Android?

I'm trying to write an app in Android Studio that opens multiple music files and stores their paths. At the moment all I'm doing is loading one file at a time which works without issue. For example - the code below shows my onclicklister for the load button and associated code. Some of the code has been simplified for this example. A user clicks the load button in the app and then uses whatever file manager they have installed to select a file which then passes the Uri back to my app. All well and good. However, is it possible to select multiple files and have them passed back to my app as an array of files? So instead of selecting a single audio file maybe the user selects 5 or 6.
If so how do you do this? Many thanks.
Anyway - what I
final View.OnClickListener mGlobal_OnClickListener = new View.OnClickListener() {
public void onClick(final View v) {
int resID2 = v.getId();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
try {
startActivityForResult(intent,resID2); }
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Please install a file manager",Toast.LENGTH_LONG).show();
}
}
};
public void onActivityResult(int requestCode, int resultCode, Intent result) {
if (resultCode == RESULT_OK)
{
Uri data = result.getData();
String thePath = data.getPath();
// Do something with the file path
}
}
You will have to put an extra value to your intent inorder to create a chooser to pick multiple files.
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("audio/*");
chooseFile.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(chooseFile, "Choose a file") , 2);
Note: Above method will work only of API level 18 and above. To support API level < 18 in your app, use some library project like Android Multiple File Selector Dialog.

Categories