Search code examples
javaandroidandroid-activitybroadcastreceiver

Implement onReceive() when a button in MainActivity is pressed


I've seen various people implement onReceive() within their class which extends BroadcastReceiver. But is there a way to implement onReceive() in a different class than MainActivity, when a button in pressed in the MainActivity? If that's possible, how would I call onReceive() when the button is pressed? I have a receiver implemented in my AndroidManifest file as well, so will that be triggered also, when the button is pressed and onReceive() is called?

MainActivity class -

public class MainActivity extends Activity {

Button activateButton;
LocationManager mManager;
AlertDialog.Builder box;
BroadcastReceiver b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    activateButton = (Button)findViewById(R.id.activate);
    activateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                DialogBox();
        }
    });

}

protected void DialogBox() {
    box = new AlertDialog.Builder(this);
    box.setTitle("Reject incoming calls?").
            setMessage("On activation, your phone will reject all incoming calls").setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent("android.intent.action.PHONE_STATE");
                    MainActivity.this.sendBroadcast(intent);
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    final AlertDialog alert = box.create();
    alert.show();

}

RejectCall class -

public class RejectCall extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    Log.i("RejectClass", "Triggered");
    ITelephony telephonyService;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    try {
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        telephonyService = (ITelephony) m.invoke(tm);
        //telephonyService.silenceRinger();
        telephonyService.endCall();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

AndroidManifest.xml -

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.scimet.admin.driveon" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name=".RejectCall">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
    </receiver>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I've defined an interface for ITelephony as well.


Solution

  • You can implement dialog to save preferences for rejecting the calls:

      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    
        protected void DialogBox() {
            box = new AlertDialog.Builder(this);
            box.setTitle("Reject incoming calls?").
                    setMessage("On activation, your phone will reject all incoming calls").setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                             preferences.edit().putBoolean(PREF_REJECT_CALLS, true).commit();
                        }
                    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     preferences.edit().putBoolean(PREF_REJECT_CALLS, false).commit();                
                     dialog.cancel();
                }
            });
            final AlertDialog alert = box.create();
            alert.show();
    
        }
    
    
    
    public class RejectCall extends BroadcastReceiver {
    
    public void onReceive(Context context, Intent intent) {
    
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    
        // If the value in preferences is false, do not reject the calls
        if(!preferences.getBoolean(PREF_REJECT_CALLS, false)){
           return;
        }
    
        Log.i("RejectClass", "Triggered");
        ITelephony telephonyService;
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Class c = Class.forName(tm.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            telephonyService = (ITelephony) m.invoke(tm);
            //telephonyService.silenceRinger();
            telephonyService.endCall();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }