gpt4 book ai didi

android - 如何禁用广播接收器以停止接收来电?

转载 作者:行者123 更新时间:2023-11-29 01:08:59 25 4
gpt4 key购买 nike

我遇到了禁用广播接收器的问题。广播接收器接收传入和传出的调用。在我的例子中,当开关打开时,接收器应该接收数据,当开关关闭时,接收器应该停止接收数据。

     switches.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

// sharedPreferences = getApplicationContext().getSharedPreferences("enableApp", Context.MODE_PRIVATE);
// SharedPreferences.Editor editor = sharedPreferences.edit();
// editor.putBoolean(getString(R.string.enable), isChecked);
// editor.commit();


if(isChecked)
{

Toast.makeText(getApplicationContext(), "Enabled", Toast.LENGTH_SHORT).show();
}
else
{
PackageManager pm = DashBoardActivity.this.getPackageManager();
ComponentName componentName = new ComponentName(DashBoardActivity.this, CallReceiver.class);
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(getApplicationContext(), "cancelled", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Disabled", Toast.LENGTH_SHORT).show();


}
}
});

这是我试过的代码,我尝试使用包管理器禁用广播接收器。

The broadcast receiver registered in manifestfile
<receiver android:name=".receiver.CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>

我想在我的 Activity 中禁用接收器。如何禁用接收器?

最佳答案

试试这个,下面的代码可以解决你的问题,

 switches.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

sharedPreferences = getApplicationContext().getSharedPreferences("enableApp", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(getString(R.string.enable), isChecked);
editor.commit();


if(isChecked)
{
PackageManager pm = DashBoardActivity.this.getPackageManager();
ComponentName componentName = new ComponentName(DashBoardActivity.this, CallReceiver.class);
int status = getApplicationContext().getPackageManager().getComponentEnabledSetting(componentName);
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Log.e("Broadcast status",status + "");
Toast.makeText(getApplicationContext(), "Enabled", Toast.LENGTH_SHORT).show();


}
else
{


PackageManager pm = DashBoardActivity.this.getPackageManager();
ComponentName componentName = new ComponentName(DashBoardActivity.this, CallReceiver.class);
int status = getApplicationContext().getPackageManager().getComponentEnabledSetting(componentName);
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Log.e("Broadcast status",status + "");
Toast.makeText(getApplicationContext(), "cancelled", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Disabled", Toast.LENGTH_SHORT).show();


}
}
});

关于android - 如何禁用广播接收器以停止接收来电?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44691894/

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