Created
March 12, 2014 19:20
-
-
Save loopiezlol/9514314 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.testcall; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentActivity; | |
import android.telephony.PhoneStateListener; | |
import android.telephony.TelephonyManager; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
/** | |
* Created by loopiezlol on 3/12/14. | |
*/ | |
public class ContactFragment extends FragmentActivity { | |
final Context context = this; | |
private Button button1; | |
private Button button2; | |
private Button button3; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.contact_fragment); | |
button1 = (Button) findViewById(R.id.button1); | |
button2 = (Button) findViewById(R.id.button2); | |
button3 = (Button) findViewById(R.id.button3); | |
// add PhoneStateListener | |
PhoneCallListener phoneListener = new PhoneCallListener(); | |
TelephonyManager telephonyManager = (TelephonyManager) this | |
.getSystemService(Context.TELEPHONY_SERVICE); | |
telephonyManager.listen(phoneListener, | |
PhoneStateListener.LISTEN_CALL_STATE); | |
// add button listener | |
button1.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
Intent callIntent = new Intent(Intent.ACTION_CALL); | |
callIntent.setData(Uri.parse("tel:0766471761")); | |
startActivity(callIntent); | |
} | |
}); | |
button2.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
Intent callIntent = new Intent(Intent.ACTION_CALL); | |
callIntent.setData(Uri.parse("tel:0741401091")); | |
startActivity(callIntent); | |
} | |
}); | |
button3.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
Intent callIntent = new Intent(Intent.ACTION_CALL); | |
callIntent.setData(Uri.parse("tel:0747640445")); | |
startActivity(callIntent); | |
} | |
}); | |
} | |
private class PhoneCallListener extends PhoneStateListener { | |
private boolean isPhoneCalling = false; | |
String LOG_TAG = "LOGGING 123"; | |
@Override | |
public void onCallStateChanged(int state, String incomingNumber) { | |
if (TelephonyManager.CALL_STATE_RINGING == state) { | |
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); | |
} | |
if (TelephonyManager.CALL_STATE_OFFHOOK == state) { | |
Log.i(LOG_TAG, "OFFHOOK"); | |
isPhoneCalling = true; | |
} | |
if (TelephonyManager.CALL_STATE_IDLE == state) { | |
Log.i(LOG_TAG, "IDLE"); | |
if (isPhoneCalling) { | |
Log.i(LOG_TAG, "restart app"); | |
Intent i = getBaseContext().getPackageManager() | |
.getLaunchIntentForPackage( | |
getBaseContext().getPackageName()); | |
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
startActivity(i); | |
isPhoneCalling = false; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment