How can we start the bluetooth adapter in background without the validation popup ?
normal way :
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
context.startActivityForResult(enableBtIntent, 1);
context.sendBroadcast(enableBtIntent);
If you want to enable bluetooth without using an Intent, you can try this:
BluetoothAdapter.getDefaultAdapter.enable();
you also need to add the BLUETOOTH and BLUETOOTH_ADMIN permission to your manifest file.
Related
I am developing a video player app and I want my app to Prompt for the permission to be the default Video Player to be the default handler of video play.
I know it has to be something like the default handler:
Intent setSmsAppIntent =
new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
setSmsAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
getPackageName());
startActivityForResult(setSmsAppIntent, your-result-code);`
source: Android Docs
But I don't know the Action I have to give to the Intent as action. Can someone please help me?
Thanks in advance.
It's not possible, developers can't call intent to change default Video player only users can do that on their device. Its only possible for SMS service.
However, for video player you can register app for video player in AndroidManifest.xml and call the function which opens settings to change default apps using the following code:
Intent intent = new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
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);
I have an app that does a check if data services is turned on and if it is not turned let display and alert dialog demanding the user to turn the app on clicking ok. This is my code that is supposed to show the settings of the android phone
final Intent intent=new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.phone","com.android.phone.Settings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
The above code does not display the option to turn on the wifi of the user
Please why the wifi data not turned on, on clicking the button
To enable the option to allow the user to turn on data services such wifi and others.
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
Just call the generic Settings screen (ACTION_SETTINGS)
Reference link http://developer.android.com/reference/android/provider/Settings.html
That will do the trick
I'm sure you all know that if you run the below code under any android.
It will enable your Bluetooth. But first, it will show you an alert showing a dialog Yes/No.
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
My question is quite simple.
How to force Yes selection on that dialog?
My purpose is to programatically force enabling the bluetooth, and any other alert Yes/No.
So the user no need to see Yes/No. They will use Yes only. no dialog.
Please share a bit of guide. Thanks.
You can try to enable BluetoothAdapter (more info) like this:
BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
mAdapter.enable()
It requires the BLUETOOTH_ADMIN permission
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)
}