Finish() or moveTaskToBack() doesn't work - android

I'm trying to put an activity in the background or simply kill it. I'm creating the activity inside a runnable, because I need to wait 2 seconds before it opens:
case TelephonyManager.CALL_STATE_RINGING:
long user_logged = AppSettings.getLoggedUserSQLiteID(context);
if (user_logged != 0) {
final Handler h = new Handler();
Runnable r1 = new Runnable() {
#Override
public void run() {
String phoneNr = inte.getStringExtra("incoming_number");
Intent phonecall = new Intent(ctx, PhoneCall.class);
phonecall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
phonecall.putExtra("phone_number", phoneNr);
ctx.startActivity(phonecall);
}
};
h.postDelayed(r1, 2000); // 1 second delay
}
break;
In the button I specify the method to run on click, like android:onClick="acceptCall":
public void acceptCall(View v) {
try {
Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
// Runtime.exec(String) had an I/O problem, try to fall back
String enforcedPerm = "android.permission.CALL_PRIVILEGED";
Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK));
Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK));
this.sendOrderedBroadcast(btnDown, enforcedPerm);
this.sendOrderedBroadcast(btnUp, enforcedPerm);
super.finish();
}
}
To finish, or put it in the background, I tried these lines of code:
super.finish();
super.moveTaskToBack(true);
this.finish();
this.moveTaskToBack(true);
However, none of them seems to work for me. Can anyone help me understand why?

In default case to kill activity, we should use:
MyActivity is your activity.
MyActivity.this.finish();
//try to kill your activity after this line of your code:
ctx.startActivity(phonecall);

Related

How pragmatically receive the recipient call without user interaction in Android

I want to write an Android code which automatically receive recipient phone call without user interaction?
Is there anyway/possibility to do this or I am just wasting of my precious time?
As I have google but not found any related result?
I tried to implement same thing and Its work perfectly but for some devices it doesn't work..
private void acceptCall() {
new Thread(new Runnable() {
#Override
public void run() {
try {
System.out.println("execute input keycode headset hook");
System.out.println("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
// Runtime.exec(String) had an I/O problem, try to fall back
System.out.println("send keycode headset hook intents");
String enforcedPerm = "android.permission.CALL_PRIVILEGED";
Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
sendOrderedBroadcast(btnDown, enforcedPerm);
sendOrderedBroadcast(btnUp, enforcedPerm);
System.out.println("Exception " + e.toString());
}
}
}).start();
}

How to Answer and Disconnect a call in android

My requirement is Accept and Disconnect a incoming call.
It is working fine If Call is Accepted and then wait for 3 seconds(handler.postDelayed) and then Disconnecting.
But I wanna disconnect the call immediately after accepting.(Dont wanna wait for 3 sconds)
Here is my code. Your help is appreciated.
public void acceptAndDissConnectCall(final Context activity) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
Log.i(TAG, "It is LOLLIPOP or Greater====");
answerCall(activity);
} else{
// do something for phones running an SDK before lollipop
Log.i(TAG, "This device is prior to LOLLIPOP ====");
acceptCall(activity);
}
dissConnectCall(activity);
}
public boolean dissConnectCall(final Context activity) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
try {
Log.d(TAG, "Dissconnect call ===== ");
// Get the boring old TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
// Get the getITelephony() method
Class classTelephony = Class.forName(telephonyManager.getClass().getName());
Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
// Ignore that the method is supposed to be private
methodGetITelephony.setAccessible(true);
// Invoke getITelephony() to get the ITelephony interface
Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
// Get the endCall method from ITelephony
Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
// Invoke endCall()
methodEndCall.invoke(telephonyInterface);
} catch (Exception ex) { // Many things can go wrong with reflection calls
ex.printStackTrace();
// return false;
}
// return true;
}
}, 4000);
return false;
}
public static void acceptCall(Context context){
Log.d(TAG, "accept the call ====");
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,
new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
}
public void answerCall(final Context mContext) {
new Thread(new Runnable() {
#Override
public void run() {
try {
Log.d(TAG, "Answer the call ====");
Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
Log.e(TAG, "IOException on answerCall ========== ");
// Runtime.exec(String) had an I/O problem, try to fall back
String enforcedPerm = "android.permission.CALL_PRIVILEGED";
Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
mContext.sendOrderedBroadcast(btnDown, enforcedPerm);
mContext.sendOrderedBroadcast(btnUp, enforcedPerm);
}
}
}).start();
}
In your dissConnectCall method change value 4000 in run method to lowest.
handler.postDelayed(new Runnable() {
#Override
public void run() {
.......
}
}, 4000);//decrease this value to 100

Intent can't move to another activity using IF

I'm having a problem with my program. The process is when the client send a message,the server sends back a message, if the message is "OK", the client will move to the next activity but it didn't. This is my code:
OnClickListener SendOnClickListener = new OnClickListener() {
public void onClick(View v) {
thread.SendSocket(Message.getText().toString());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Actions to do after 3 seconds
if(Response.getText().toString().equals("OK")){
Intent i = new Intent(MainActivity.this,LOGIN.class);
startActivity(i);
}
Response.setText(thread.Socketdata );
}
}, 3000);
}
};
Change your if condition as
if(thread.Socketdata.toString().equals("OK")){
Intent i = new Intent(MainActivity.this,LOGIN.class);
startActivity(i);
}
and check.

Give some delay after calling an Intent in Android

I want to add some delay after calling an intent from a service. Ex: as shown below
How can i do it?
Any Help would be appreciated.
if (yAccel > 500 || yAccel < -500)
{
gMeasure = true;
lockStatus = 1; //lock done;
Intent intent = new Intent(this, ScreenLock.class);
startActivity(intent);
//delay for min 10 min
}
Have you tried with an Handler?
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(this, ScreenLock.class);
startActivity(intent);
}
}, 1000);

Android Test Framework performClick() not starting Activity

I have an ActivityInstrumentationTestCase2 with a test executing Button.click(). The Button should start an other Activity to do some work.
I think Button.performClick() is performed correctly, but the test is finishing before the other Activity is executed.
#UiThreadTest
public void test() {
Intent i = new Intent(this.myActivity, MyActivity.class);
myActivity.startActivity(i);
Button button = (Button) myActivity.findViewById(R.id.button);
button.performClick();
}
I tried the following which worked but i think this is rather a work-around than a good solution.
public void test() {
Intent i = new Intent(this.myActivity, MyActivity.class);
myActivity.startActivity(i);
Button button = (Button) myActivity.findViewById(R.id.button);
button.performClick();
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
Log.e("MyTest", e.getMessage());
}
}
IsnĀ“t there a better way?
This is my final solution:
public void test() {
Instrumentation instrumentation = getInstrumentation();
// Prepare a monitor for your activity
Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(MyActivity.class.getName(), null, false);
// Start your activity manually
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(instrumentation.getTargetContext(), MyActivity.class.getName());
instrumentation.startActivitySync(intent);
Activity myActivity = getInstrumentation().waitForMonitor(monitor);
Button upSend = (Button) myActivity.findViewById(R.id.button);
upSend.performClick();
Log.d("MyTest", "button clicked");
//wait for SecondActivity to start (called by MyActivity)
monitor = instrumentation.addMonitor(SecondActivity.class.getName(), null, false);
Activity secondActivity = getInstrumentation().waitForMonitor(monitor);
int count = 0;
//wait until SecondActivity is finishing
while(!secondActivity.isFinishing()) {
Log.d("MyTest", "waiting - " + ++count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e("MyTest",e.getMessage());
}
}
}
Thx again to Erik; now the second activity gets started by button and test is waiting for it to finish.

Categories

Resources