Мне нужно прослушивать входящие звонки. Я пользуюсь услугами для этого. Моя активность открывается при входящем звонке. Там есть 2 кнопки. Я хочу принять входящий звонок, когда нажата одна из этих кнопок. Я перепробовал много вещей, но ни одна из них не сработала. Я был бы рад, если вы покажете какую-либо помощь или ресурсы.
AndroidManifest. xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission
android:name="android.permission.MODIFY_PHONE_STATE"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
<activity android:name=".Activities.IncomingActivity">
<intent-filter>
<data android:mimeType="vnd.android.cursor.item/phone" />
<data android:mimeType="vnd.android.cursor.item/person" />
<data android:mimeType="vnd.android.cursor.dir/calls" />
<data android:scheme="tel" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="vnd.android.cursor.item/phone" />
<data android:mimeType="vnd.android.cursor.item/person" />
<data android:mimeType="vnd.android.cursor.dir/calls" />
<data android:scheme="tel" />
</intent-filter>
</activity>
<receiver android:name="ixa.whocalled.BroadcastReceiver.PhoneListenerBroad">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
</application>
PhoneListenerBroad - * BroadcastReceiver
public class PhoneListenerBroad extends BroadcastReceiver {
Context c;
private String outgoing;
@Override
public void onReceive(Context context, Intent intent) {
c = context;
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
outgoing = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
int state = 2;
Intent intentPhoneCall = new Intent(c, IncomingActivity.class);
intentPhoneCall.putExtra("incomingnumber", outgoing);
intentPhoneCall.putExtra("state", state);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(intentPhoneCall);
Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
}
try {
TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(final int state, final String incomingNumber) {
Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable() {
@Override
public void run() {
if (state == 1) {
Intent intentPhoneCall = new Intent(c, IncomingActivity.class);
intentPhoneCall.putExtra("incomingnumber", incomingNumber);
intentPhoneCall.putExtra("state", state);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(intentPhoneCall);
}
}
};
if (state == 1) {
callActionHandler.postDelayed(runRingingActivity, 100);
}
if (state == 0) {
callActionHandler.removeCallbacks(runRingingActivity);
}
}
}
}
IncomingActivity
public class IncomingActivity extends AppCompatActivity {
Button Accept_Button, Decline_Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_incoming);
Accept_Button = findViewById(R.id.ai_button01);
Decline_Button = findViewById(R.id.ai_button02);
Accept_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
acceptCall();
}
});
Decline_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//empty
}
});
}
public void acceptCall() {
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 0);
headSetUnPluggedintent.putExtra("name", "Headset");
try {
sendOrderedBroadcast(headSetUnPluggedintent, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}