Java 按下MainActivity中的按钮时执行onReceive()

Java 按下MainActivity中的按钮时执行onReceive(),java,android,android-activity,broadcastreceiver,Java,Android,Android Activity,Broadcastreceiver,我见过很多人在他们的类中实现了onReceive(),该类扩展了BroadcastReceiver。但是,当按下MainActivity中的按钮时,是否有方法在不同于MainActivity的类中实现onReceive()?如果可能的话,按下按钮时如何调用onReceive()?我在我的AndroidManifest文件中也实现了一个接收器,所以当按下按钮并调用onReceive()时,它也会被触发吗 主要活动类- public class MainActivity extends Activi

我见过很多人在他们的类中实现了
onReceive()
,该类扩展了
BroadcastReceiver
。但是,当按下
MainActivity
中的按钮时,是否有方法在不同于
MainActivity
的类中实现
onReceive()
?如果可能的话,按下按钮时如何调用
onReceive()
?我在我的
AndroidManifest
文件中也实现了一个接收器,所以当按下按钮并调用
onReceive()
时,它也会被触发吗

主要活动类-

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();

}
拒绝呼叫类-

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>


我还为ITelephony定义了一个接口。

您可以实现对话框来保存拒绝调用的首选项:

  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();
    }

}

嗨,我的手机就这样死机了。手机重新启动。我将编辑描述并发布我的代码。请参阅我的编辑,您需要保存首选项以打开或关闭拒绝。当接收到调用时,广播由系统发送。它不应该是“preferences.edit().putBoolean(“PREF_REJECT_CALLS”,true)。commit();”吗对不起,我可能完全错了。还有,这个如何保存首选项?另外,我使用了SharedReferences。有什么问题吗?这可能就是为什么我必须使用putBoolean()你是对的,它是putBoolean,你可以使用SharedPreferences。我是凭记忆写的。我会编辑这篇文章。