从广播接收器关闭活动
Posted
技术标签:
【中文标题】从广播接收器关闭活动【英文标题】:Close the activity from a broadcast receiver 【发布时间】:2018-03-13 06:38:29 【问题描述】:我的项目是关于通话记录器的。 我想在接到电话时,用一个按钮打开我的透明活动。 我从广播接收器类打开我的活动。 在此活动中,我有一个切换按钮来激活或停用我的应用程序。 接到电话时,如果我的切换按钮打开,它就会开始记录通话。 它工作正常,但通话结束后,我从广播接收器类中关闭我的活动。 像这样:首先,这是我接到电话时的活动:
public class Test extends Activity
static Test tes;
ToggleButton toggleButton;
TextView txt_disable, txt_enable;
private static Context context;
private BroadcastReceiver _closeActivityReceiver = new CloseActivityReceiver();
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
tes = this;
IntentFilter filter = new IntentFilter("closeNoInternetActivity");
this.registerReceiver(_closeActivityReceiver, filter);
toggleButton = findViewById(R.id.togglebtn);
txt_enable = findViewById(R.id.txt_enable);
txt_disable = findViewById(R.id.txt_disable);
if (isActivityRunning(MainActivity.class))
MainActivity.getInstance().finish();
final SharedPreferences pref = getSharedPreferences("TOGGLE", MODE_PRIVATE);
boolean sie = pref.getBoolean("STATE", true);
if (sie == true)
Log.i("mhs", "true");
//to main activity toggleRecord fal hast pass inja user agar bekhad mitone k disable kone
toggleButton.setChecked(true);
toggleButton.setText(null);
toggleButton.setTextOn(null);
toggleButton.setTextOff(null);
toggleButton.setBackgroundResource(R.drawable.record_btn);
txt_disable.setVisibility(View.VISIBLE);
toggleButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
toggleRecord.setChecked(false);
toggleButton.setBackgroundResource(R.drawable.not_recording_btn);
finishAffinity();
);
else
Log.i("mhs", "false");
toggleButton.setChecked(false);
toggleButton.setText(null);
toggleButton.setTextOn(null);
toggleButton.setTextOff(null);
toggleButton.setBackgroundResource(R.drawable.not_recording_btn);
txt_enable.setVisibility(View.VISIBLE);
toggleButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
toggleRecord.setChecked(true);
toggleButton.setBackgroundResource(R.drawable.record_btn);
finishAffinity();
);
protected Boolean isActivityRunning(Class activityClass)
ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks)
if (activityClass.getCanonicalName().equalsIgnoreCase(task.baseActivity.getClassName()))
return true;
return false;
private class CloseActivityReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
// close this damn activity!
@Override
public void onDestroy()
super.onDestroy();
this.unregisterReceiver(this._closeActivityReceiver);
public static Test getInstance()
return tes;
这是我的接收器: 在这里,当我有电话时,我会像这样从接收器开始活动
switch (state)
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallStarted(context, number, callStartTime);
Intent i = new Intent(context, Test.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Log.i("mhs","zang khord");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (lastState != TelephonyManager.CALL_STATE_RINGING)
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
Log.i("mhs","ghat kard");
else
isIncoming = true;
callStartTime = new Date();
onIncomingCallAnswered(context, savedNumber, callStartTime);
//vaghti zang mikhore va ghat mikone dar akhar
Log.i("mhs","ghatid");
Intent in = new Intent("closeNoInternetActivity");
context.sendBroadcast(in);
我在这里关闭它:
case TelephonyManager.CALL_STATE_OFFHOOK:
if (lastState != TelephonyManager.CALL_STATE_RINGING)
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
Log.i("mhs","ghat kard");
else
isIncoming = true;
callStartTime = new Date();
onIncomingCallAnswered(context, savedNumber, callStartTime);
//vaghti zang mikhore va ghat mikone dar akhar
Log.i("mhs","ghatid");
Intent in = new Intent("closeNoInternetActivity");
context.sendBroadcast(in);
我的问题是我认为我的活动没有关闭,它在后台提醒并导致我的活动是透明的,然后我又有一个电话。我的振铃屏幕没有显示在屏幕上。 我想如果我能够以正确的方式关闭我的测试活动,我的问题就解决了。
【问题讨论】:
最重要的是:您的BroadcastReceiver
是否收到Intent
?
我从我的 BroadcastReceiver 开始活动并再次完成。
使用 BroadcastReceiver 然后广播一个本地消息,Activity 可以注册一个侦听器,然后完成 Activity 的代码可以保留在 Activity 中而不是尝试在外部关闭它,这可能是有意义的.你试过这种方法吗?
我在我的活动中注册我的接收器
【参考方案1】:
只需复制并粘贴以下代码即可。它应该工作。如果没有,请尝试修改完成方法,如果需要,您可以显式调用 destroy
方法。
public class Test extends Activity
static Test tes;
private static Context context;
ToggleButton toggleButton;
TextView txt_disable, txt_enable;
private BroadcastReceiver _closeActivityReceiver = new CloseActivityReceiver();
public static Test getInstance()
return tes;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
tes = this;
IntentFilter filter = new IntentFilter("closeNoInternetActivity");
this.registerReceiver(_closeActivityReceiver, filter);
toggleButton = findViewById(R.id.togglebtn);
txt_enable = findViewById(R.id.txt_enable);
txt_disable = findViewById(R.id.txt_disable);
if (isActivityRunning(MainActivity.class))
MainActivity.getInstance().finish();
final SharedPreferences pref = getSharedPreferences("TOGGLE", MODE_PRIVATE);
boolean sie = pref.getBoolean("STATE", true);
if (sie == true)
Log.i("mhs", "true");
//to main activity toggleRecord fal hast pass inja user agar bekhad mitone k disable kone
toggleButton.setChecked(true);
toggleButton.setText(null);
toggleButton.setTextOn(null);
toggleButton.setTextOff(null);
toggleButton.setBackgroundResource(R.drawable.record_btn);
txt_disable.setVisibility(View.VISIBLE);
toggleButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
toggleRecord.setChecked(false);
toggleButton.setBackgroundResource(R.drawable.not_recording_btn);
finishAffinity();
);
else
Log.i("mhs", "false");
toggleButton.setChecked(false);
toggleButton.setText(null);
toggleButton.setTextOn(null);
toggleButton.setTextOff(null);
toggleButton.setBackgroundResource(R.drawable.not_recording_btn);
txt_enable.setVisibility(View.VISIBLE);
toggleButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
toggleRecord.setChecked(true);
toggleButton.setBackgroundResource(R.drawable.record_btn);
finishAffinity();
);
protected Boolean isActivityRunning(Class activityClass)
ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks)
if (activityClass.getCanonicalName().equalsIgnoreCase(task.baseActivity.getClassName()))
return true;
return false;
@Override
public void onDestroy()
super.onDestroy();
this.unregisterReceiver(this._closeActivityReceiver);
public void finish()
super.finish();
private class CloseActivityReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
// close this damn activity!
finish();
【讨论】:
没什么变化,我的活动仍未结束。我无法理解您的意思是“它应该可以工作。如果没有,请尝试修改完成方法,如果需要,您可以显式调用销毁方法。” 您能否提供更多信息,例如您从哪里发送广播以及何时发送?编辑您的问题* 在我的广播中当我有电话时,我开始我的活动,当电话结束时,我从广播中结束我的活动 尝试调试您的代码并查看是否已调用onReceive()
方法。当应用程序处于后台或被破坏时,请举杯并检查这两种情况。以上是关于从广播接收器关闭活动的主要内容,如果未能解决你的问题,请参考以下文章