gpt4 book ai didi

java - 当按下 MainActivity 中的按钮时实现 onReceive()

转载 作者:行者123 更新时间:2023-12-01 12:28:10 25 4
gpt4 key购买 nike

我见过很多人在他们的类中实现了onReceive(),该类扩展了BroadcastReceiver。但是,当在 MainActivity 中按下按钮时,有没有办法在与 MainActivity 不同的类中实现 onReceive() 呢?如果可能的话,按下按钮时如何调用 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 定义了一个接口(interface)。

最佳答案

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

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

}

关于java - 当按下 MainActivity 中的按钮时实现 onReceive(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26168334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com