Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- String MANUFACTURER_SAMSUNG = "SAMSUNG";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- }
- public class PhoneStateReceiver extends BroadcastReceiver {
- public void onReceive(Context context, Intent intent) {
- Log.d("in","phone state onreceive");
- try {
- System.out.println("Receiver start");
- String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
- String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
- if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
- Toast.makeText(context, "Incoming Call State", Toast.LENGTH_SHORT).show();
- Toast.makeText(context, "Ringing State Number is -" + incomingNumber, Toast.LENGTH_SHORT).show();
- Log.d("after","toasts");
- MediaSessionManager mediaSessionManager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
- Log.d("before","tryblock");
- try {
- List<MediaController> mediaControllerList = mediaSessionManager.getActiveSessions(new ComponentName(context, NotificationReceiverService.class));
- Log.d("medialistempty",mediaControllerList.isEmpty()+"");
- for (MediaController m : mediaControllerList) {
- Log.d("packagename:",m.getPackageName());
- if ("com.android.server.telecom".equals(m.getPackageName())) {
- m.dispatchMediaButtonEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
- Log.d("HEADSETHOOK sent", "to telecom server");
- break;
- }
- }
- } catch (SecurityException e) {
- Log.d("Permission error.","Access to notification not granted to the app.");
- Log.d("exc",e.toString());
- }
- TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
- try {
- if (tm == null) {
- // this will be easier for debugging later on
- throw new NullPointerException("tm == null");
- }
- // do reflection magic
- tm.getClass().getMethod("answerRingingCall").invoke(tm);
- } catch (Exception e) {}
- }
- if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))) {
- Toast.makeText(context, "Call Received State", Toast.LENGTH_SHORT).show();
- try {
- Intent callIntent = new Intent(Intent.ACTION_CALL);
- callIntent.setData(Uri.parse("tel:" + "611,,,1"));
- context.startActivity(callIntent);
- }catch(SecurityException e) {
- Log.d("myexcpetion","Security exception");
- }
- }
- if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
- Toast.makeText(context, "Call Idle State", Toast.LENGTH_SHORT).show();
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- public class NotificationReceiverService extends NotificationListenerService{
- PhoneStateReceiver psr;
- public void onCreate(){
- super.onCreate();
- psr = new PhoneStateReceiver();
- IntentFilter filter = new IntentFilter();
- filter.addAction("diverse.technologies.autocallreceiver.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
- registerReceiver(psr,filter);
- }
- public void onDestroy(){
- super.onDestroy();
- unregisterReceiver(psr);
- }
- public NotificationReceiverService() {}
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="diverse.technologies.autocallreceiver">
- <uses-permission android:name="android.permission.WAKE_LOCK" />
- <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
- <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <uses-permission android:name="android.permission.CALL_PHONE" />
- <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter android:priority="100" >
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- <action android:name="android.intent.action.MAIN" />
- <action android:name="android.intent.action.DIAL" />
- <action android:name="android.intent.action.CALL_PRIVILEGED" />
- <data android:scheme="tel" />
- </intent-filter>
- </activity>
- <receiver android:name=".PhoneStateReceiver">
- <intent-filter>
- <action android:name="android.intent.action.PHONE_STATE" />
- </intent-filter>
- </receiver>
- <service android:name=".NotificationReceiverService" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
- android:enabled="true" android:exported="true" >
- <intent-filter>
- <action android:name="android.service.notification.NotificationListenerService" />
- </intent-filter>
- </service>
- </application>
- </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement