我想从第一个活动中打开一个活动,并且我想有意识地关闭第一个活动。我试过这个,但接收器不起作用。而且我的应用程序中有不同的接收器,所以我希望这个意图只从 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;
}
}