我想打开第一个活动,我想以意图关闭第一个活动。我尝试过这个,但是接收器不起作用。我的应用程序中有不同的接收器,所以我希望这个意图仅从FirstReceiver接收。我该怎么办?
public class First extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Intent close = new Intent(getApplicationContext(), Close.class);
startActivity(close);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
class FirstReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.e("FirstReceiver","FirstReceiver");
First.this.finish();
}
}
}
这是第二堂课。
public class Close extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_close);
Intent myIntent = new Intent();
sendBroadcast(myIntent);
Log.e("onCreate","onCreate");
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.close, menu);
return true;
}
}
答案 0 :(得分:10)
这可能对你有帮助......
public class First extends Activity {
public static final String ACTION_CLOSE = "yourPackageName.ACTION_CLOSE";
private FirstReceiver firstReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
IntentFilter filter = new IntentFilter(ACTION_CLOSE);
firstReceiver = new FirstReceiver();
registerReceiver(firstReceiver, filter);
Intent close = new Intent(getApplicationContext(), Close.class);
startActivity(close);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(firstReceiver);
}
class FirstReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("FirstReceiver", "FirstReceiver");
if (intent.getAction().equals(ACTION_CLOSE)) {
First.this.finish();
}
}
}
}
public class Close extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_close);
Intent myIntent = new Intent(First.ACTION_CLOSE);
sendBroadcast(myIntent);
Log.e("onCreate", "onCreate");
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.close, menu);
return true;
}
}
答案 1 :(得分:2)
因为最好使用startActivityForResult,onActivityRsult()
Intent in = new Intent(getApplicationContext(), Close.class);
startActivityForResult(in, RESULT_CLOSE);
并覆盖onActivityResult
中的Activity
并按此实施..
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_CLOSE){
finish();
}
如果要关闭mainActivity,请在关闭活动中调用setResult()
..
答案 2 :(得分:1)
当用户希望退出所有打开的活动时,他们应该按下一个按钮,该按钮会加载应用启动时运行的第一个活动,在我的情况下为“关闭”。
Intent intent = new Intent(getApplicationContext(), Close.class);
intent.putExtra("EXIT", true);
startActivity(intent);
上述代码会清除除关闭以外的所有活动。 关闭是用户运行程序时启动的第一个活动。然后将此代码放在关闭的onCreate中,以指示当“Exit”消息通过时它应该自毁。
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}