I want to creat an Android app that can get phone number from a text file then make a phone call immediatelly without clicking any extra button. But, i find no way to do that. All samples on internet use the default call button to make a phone.
here is the code i used
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CALL) {
performDial();
return true;
}
return false;
}
public void performDial(){
if(edittext.getText()!=null){
try {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + edittext.getText())));
} catch (Exception e) {
e.printStackTrace();
}
}//if
}
Thanks in advance
First, I do not know if you can get the KEYCODE_CALL event or not in onKeyDown().
Second, use ACTION_CALL instead of ACTION_DIAL. You will need to hold the CALL_PHONE permission for this to work.
It is very Simple. Do it like this ---->
Fire an intent ---->
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("Tel:(+12)123456789"));
startActivity(intent);
Add this permission to your manifest file ----->
And your ready to go....
Related
How can we perform a call to any number when application is in the background using volume key (number set by own self-using application)?
When I press up the volume key 3 times, I want the app to call my Dad's number even if the app is in background.
Using following code you can achieve it -
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));
context.startActivity(intent);
Also, don't forget to add the permission for the same in Manifest file -
uses-permission android:name="android.permission.CALL_PHONE" />
And lastly you need to check for the volume up button event to trigger the call -
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
down = false;
} else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
up = false;
}
return true;
}
Based on the condition you can achieve the same. Thanks :)
We have a launcher application that works fine on older versions of Android. We have a device that is running Android 5.1, and are running into issues.
When pressing the back button from within the application, we allow the user to go to the settings page. Pressing the home key re-launches the application. Pressing the back button on other devices also relaunches the application.
On the new device, pressing the back button allows us to navigate to the Android home page. It does not launch the application.
We are overriding the back button like so:
#override
public void onBackPressed() {
// Display the password prompt if required
if (PreferencesManager.isPasswordPresent()) {
LeaveApplicationPasswordDialogFragment dialog = LeaveApplicationPasswordDialogFragment.getInstance();
dialog.show(getSupportFragmentManager(), "password");
}
else {
// Prompt whether we are about to leave the app
LeaveApplicationDialogFragment dialog = null;
MyApplication application = (MyApplication )
getApplication();
if (application.isDefaultLauncher()) {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ** to access the device's settings?");
}
else {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ***");
}
dialog.show(getFragmentManager(), "leaving");
}
}
In the dialog fragment, we accept the confirmation and process it like so:
public void exitToSettings() {
GUIAndroidTouchBaseActivity.this.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
shutdownOperations();
finish();
}
Per some research and other threads, I worked with our exit method like so:
public void exitToSettings() {
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
GUIAndroidTouchBaseActivity.this.startActivity(intent);
shutdownOperations();
finish();
}
No dice. Same behavior.
What am I missing? Is there something in OS 5.1 that's overriding our launcher? Again, pressing the home button launches the app as expected. Navigating to the home page from the settings page by pressing the back button does not.
What we have works on other devices and OSs. We've had no issue with 4.1 and 6.1.
We are also overriding the back button like so:
#Override
public boolean onKeyDown(int keyCode, KeyEvent KEvent) {
int deviceid = KEvent.getDeviceId();
//Making sure not processing same key again
if (KEvent.getRepeatCount() != 0) {
return true;
}
if (!SettingsOpened) {
int keyaction = KEvent.getAction();
// "Esc" key can not be stooped id diveceid is non zero because it can be back key of android
if (KEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && deviceid != 0) {
return super.onKeyDown(keyCode, KEvent);
}
if (keyaction == KeyEvent.ACTION_DOWN) {
String key = KeyEvent.keyCodeToString(keyCode); //wont work in version 11 or less
if (keyCode != KeyEvent.KEYCODE_ENVELOPE) {
Matcher matcher = KEYCODE_PATTERN.matcher(key);
if (matcher.matches() || ExternalKeyboard.keyMatches(KEvent)) {
int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState());
char character = (char) keyunicode;
//toast.makeText(this, "onKeyDown" + _lastChar + repeatcount, toast.LENGTH_SHORT).show();
_lastChar = character;
_actionDown = true;
ExternalKeyboard.KeyboardAddChar(character);
}
}
}
return true;
}
else {
return super.onKeyDown(keyCode, KEvent);
}
}
Thanks!
Adding
android:stateNotNeeded="true"
android:clearTaskOnLaunch="false"
to my manifest took care of it.
I'm new here.
I have a problem, i try to shutdown a 4.2.2 android device (not root).
I know that ACTION_SHUTDOWN not works with unroot device.
So i want to open the existing shutdown/reboot/airplane dialog, the same we get when we maintain the on/off button. Then the user just have to click shutdown button.
I try to create by this way, without result...
Intent intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS); // or others settings
startActivity(intent);
Thanks,
The is no public sdk access to open the power button menu programatically.
This link has all the approches Here.Simulating power button press to display switch off dialog box
InputManager.getInstance().injectInputEvent(new InputEvent(KeyEvent.KEYCODE_POWER, keyCode), sync);
'sync' becomes either of these:
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT
and you need
import android.hardware.input.InputManager;
This is untested, but puts you in the right direction, also bare in mind, functionality like this is NOT recommend.
failing that:
public static void simulateKey(final int KeyCode) {
new Thread() {
#Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyCode);
} catch (Exception e) {
Log.e("Exception when sendKeyDownUpSync", e.toString());
}
}
}.start();
}
and simply call it like
simulateKey(KeyEvent.KEYCODE_POWER);
I have a small piece of code which is basically supposed to make a phone call when a button is pushed. I looked it up online and all the sources basically gave the same code. But for some reason this code doesn't work. It makes the app crash but the LogCat doesn't display anything (meaning the log is completely blank). I should also mention that in my manifest file I did add the following permission
<uses-permission android:name = "andriod.permission.CALL_PHONE" />
The code I have is as follows. Any help would be greatly appreciated!
phoneButton.setOnClickListener(new OnClickListener () {
public void onClick(View v) {
try {
final Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:232131232"));
ContactUs.this.startActivity(callIntent);
}catch (ActivityNotFoundException e){
Log.e("Dialing", "call Failed!", e);
}
}
});
You spelt android wrong the second time...
Sounds like you need to add the user-permission for making a phone call. I believe the permission is:
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
in your manifest file.
This is a snippet from an Activity class I am currently testing on my HTC Desire -
okButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + getPhoneNumber()));
startActivity(intent);
}
});
I suggest changing ContactUs.this.startActivity(callIntent); to startActivity(callIntent); and testing it again.
I'm a relative beginner with Android. Does anybody have a sane explanation for how to listen for keys and soft keys in an EditText/TextView?
I'd love to see a comprehensive tutorial or set of examples.
As I understand it, I can add a KeyListener to my Activity, e.g. onKeyDown(), onKeyUp() but when I try this I can't trigger the events for normal keys only HOME and BACK for example.
I have seen mention of using a TextWatcher but that isn't the same as handling raw key events.
There seem to be a number of half-solutions here on SO. Hoping you can help clear the mists of confusion...
You have to assign a key listener not to activity but rather to EditText itself.
This is what I have to listen to BACK or MENU key events. Simply add this method, without implementing any Interface. I do this in my BaseActivity, from which every Activity inherits.
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(NAME, "Key pressed");
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Log.d(NAME, "Back pressed");
// IGNORE back key!!
return true;
/* Muestra el MenĂº de Opciones */
case KeyEvent.KEYCODE_MENU:
Intent menu = new Intent(this, Menu.class);
// start activity
startActivity(menu);
return true;
}
return super.onKeyDown(keyCode, event);
}
PS: I highly discourage ignoring the back key.
For example:
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
//your code here
}
return false;
}
});
I recently found another way that be stuck using Activity onKeyDown, or event setting a key listener on view (which is not really working with key events from ADB in my case) with view.setOnKeyListener.
Since android P method addOnUnhandledKeyEventListener has been introduced. It allows you to do whatever you need to do when your view is able to catch unhandled key events.
Here is an example of how I used it :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) yourView.addOnUnhandledKeyEventListener { v, event ->
when (event.keyCode) {
KeyEvent.KEYCODE_UNKNOWN -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
KeyEvent.KEYCODE_SOFT_RIGHT -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
// etc...
else -> false // Specify you didn't handle the event
}
}