I am really in trouble with that situation. When a call comes, I can hold the call and number that call me. After this incoming calls, I want to show an "alert dialog "although my application is closed.
I am using the Android emulator with API >23.
Here is my mainactivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.READ_PHONE_STATE)) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
}
} else {
//do nothing
}
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.READ_CALL_LOG)) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CALL_LOG}, 2);
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CALL_LOG}, 2);
}
} else {
//do nothing
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted1", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "No permission granted1", Toast.LENGTH_SHORT).show();
}
return;
}
case 2: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted2", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "No permission granted2", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
My androidmanifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<application
...
<receiver android:name="InterceptCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
...
And my InterceptCall.java
public class InterceptCall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try{
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String pNum = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Toast.makeText(context,"Ringing State. Number is - "+pNum,Toast.LENGTH_SHORT).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Toast.makeText(context,"OFFHOOK// ",Toast.LENGTH_SHORT).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context,"IDLE.Call End",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
I really do not know what to do... or how to do it. I just want to show an alert dialog with yes/no button when the call has ended.
One way to create dialog when app is not open create activity use dialog theme and call it from service