我已经看到不同的人在他们的班级中实施onReceive()
,扩展了BroadcastReceiver
。但是当onReceive()
中的按钮被按下时,是否有办法在MainActivity
以外的其他班级中实施MainActivity
?如果可以,按下按钮后如何调用onReceive()
?我的AndroidManifest
文件中也有一个接收器,当按下按钮并调用onReceive()
时,它也会被触发吗?
MainActivity类 -
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类 -
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定义了一个界面。
答案 0 :(得分:1)
您可以实施对话框以保存拒绝来电的首选项:
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();
}
}