我需要从广播接收器中关闭当前活动。我不确定如何从中调用“结束”,也许有一种方法可以模拟“后退”键按下。只要能够完成工作,任何实现方式都可以。
@Override
public void onReceive(Context context, Intent intent) {
// How can I finish the current activity here?
}
YourCurrentActivityName.this.finish();
或者您可以使用this.finish();
来终止前台活动,这样最后打开的被卡住的活动就会出现在前台。
public class ActivityFirstName extends Activity {
private BroadcastReceiver mFinishReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// other code
if (mFinishReceiver == null) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.ACTION_TERMINATE");// a string to identify your action
mFinishReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// How can I finish the current activity here?
if ("com.example.ACTION_TERMINATE".equals(intent.getAction())) {
ActivityFirstName.this.finish();
}
}
};
registerReceiver(mFinishReceiver, intentFilter);
}
// other code
}
@Override
protected void onDestroy() {
super.onDestroy();
if (isFinishing()) {
if (mFinishReceiver != null) {
unregisterReceiver(mFinishReceiver);
}
}
}
}
同时,前台/当前运行的活动以及广播的发送者:
public class ActivitySecondName extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
// code code code
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action on click
terminateBackActivities();
}
});
}
private void terminateBackActivities() {
Intent i = new Intent("com.example.ACTION_TERMINATE"); // the two action strings MUST be same
// i.putExtra(...); // to send extra data
sendBroadcast(i);
}
}
public void onReceive(Context context, Intent intent)
方法中尝试使用this.finish();
,因为在这种情况下,this
指的是BroadcastReceiver
类,你将无法完成此操作。应该使用ActivityName.this.finish();
来解决问题。 - Alpay根据您的评论,如果BroadcastReceiver不是活动的内部类,则应执行以下操作:不要将广播接收器放在单独的类中,而是像这样在活动内定义:
private BroadcastReceiver mFinishReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
YourActivity.this.finish();
}
};
然后,您将希望在onResume()中注册接收器,如下所示:
@Override
public void onResume(){
super.onResume();
registerReceiver(mFinishReceiver, new IntentFilter(yourIntentAction));
}
@Override
public void onPause(){
super.onPause();
unregisterReceiver(mFinishReceiver);
}
this.finish();
。如其他答案所建议的那样,您可以在广播接收器代码中简单地调用finish()来结束活动,或者您甚至可以自己触发后退按钮按键事件。
this.dispatchKeyEvent(new Keyevent(ACTION_DOWN, KEYCODE_BACK));
不确定这对你是否有帮助,但它曾经帮助过我,我认为这里的情况也是如此,所以我来回答你。
每当广播接收器被调用时,您可以通过单击该广播消息导航到任何活动。
就像:
@Override
public void onReceive(Context context, Intent intent) {
// My Notification Code
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.app_icon;
//System.out.println("The ID Number is: "+Long.parseLong(intent.getData().getSchemeSpecificPart()) );
contentText = intent.getStringExtra("MyMessage");
System.out.println("The Message is: "+intent.getStringExtra("MyMessage"));
CharSequence text = "Your tax amount due period";
CharSequence contentTitle = "Tax Toolbox";
long when = System.currentTimeMillis();
intent = new Intent(context, MenuPageActivity.class); // here i am calling activity
intent.putExtra("sixMonth", "sixMonth");
intent.putExtra("messageSixMonth", contentText);
PendingIntent contentIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification = new Notification(icon,text,when);
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate; // To vibrate the Device
notification.ledARGB = Color.RED;
notification.ledOffMS = 300;
notification.ledOnMS = 300;
notification.defaults |= Notification.DEFAULT_LIGHTS;
//notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(com.project.TaxToolbox.NotificationConstants.NOTIFICATION_ID_SIX_MONTH, notification);
}
现在,在该活动的onCreate()中,您必须确定它是由通知调用还是不是。
就像这样:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
System.out.println("The Extra for twoMonth is: "+getIntent().hasExtra("twoMonth"));
System.out.println("The Extra for sixMonth is: "+getIntent().hasExtra("sixMonth"));
System.out.println("The Extra for EveryMonth is: "+getIntent().hasExtra("everyMonth"));
if(getIntent().hasExtra("sixMonth")){
notificationManager.cancel(NotificationConstants.NOTIFICATION_ID_SIX_MONTH);
final AlertDialog alert3 = new AlertDialog.Builder(MenuPageActivity.this).create();
alert3.setTitle("Tax Toolbox");
alert3.setMessage(getIntent().getExtras().getString("messageSixMonth"));
alert3.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alert3.setIcon(R.drawable.app_icon);
alert3.show();
// 在这里你可以做更多的事情或者关闭活动。 }
不确定,但可能对你有帮助。
如果有帮助,请随意评论。
尝试使用:
Intent i = new Intent(context,intent.getClass());
public class BaseActivity extends Activity {
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(receiver, new IntentFilter(YOUR_INTENT_FILTER));
}
/*
* (non-Javadoc)
*
* @see android.app.Activity#onStop()
*/
protected void onStop(){
unregisterReceiver(receiver);
}
/*
* (non-Javadoc)
*
* @see android.app.Activity#onStart()
*/
protected void onStart(){
super.onStart();
registerReceiver(receiver, new IntentFilter(YOUR_INTENT_FILTER));
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
/*
* (non-Javadoc)
*
* @see
* android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
onBackPressed();//on back pressed simply calls finish()
}
};
} // End of BaseActivity
// End of File
ActivityManager类可以提供当前前台活动(即使它不是来自您的应用程序)。getRunningTasks方法将给您一个正在运行的任务列表,列表的第一个元素是最近启动的活动。不幸的是,此方法只会给您一个RecentTaskInfo类型的对象,而不是活动本身,因此无法调用其finish()方法,我相信 :/
另一方面,如果您想从您的应用程序关闭当前活动,则可以在个人类上实现静态变量,每个活动都会在其onResume()方法中设置该变量。这样,您将始终知道哪个活动是当前活动。但我猜这不是您要寻找的。
编辑:如文档所述,getRunningTasks仅用于调试目的。
BroadcastReceiver
类中的 onReceive()
所有任务后,按照以下方式放置 finish();
: @Override
public void onReceive(Context context, Intent intent) {
// Do all the tasks onReceive of BroadCast Receiver
finish(); // This finishes the current activity here....
}
finish()
就可以解决问题了。 - 323go