[英]Finish All Activities from BroadCast Receiver
屏幕关闭时,我想从BroadCast Receiver中完成所有应用程序的活动:首先,我在“活动”中创建了一个静态引用,如下所示:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public static AppCompatActivity ma;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set instance of ma equals to MainActivity
ma=this;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void displayView(int viewId) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (viewId) {
case R.id.monthly_figures:
fragment = new dailysales();
title = "Daily Figures";
break;
case R.id.nav_gallery:
fragment=new monthlysales();
title="Monthly Figures";
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
那么Call来自我的BroadCastreceiver类,如下所示:
public class SCBroadcaster extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
MainActivity.ma.finish();
}
}
}
您是否认为还有另一种更好的方法? 我试图调用System.Exit(0)或process.killprocess(myPid),但是当重新打开屏幕时,它将重新启动整个应用程序。 谢谢。
Android的设置不是很好。 该框架的想法是该应用程序永远都不应关闭,并且始终可以在同一位置重新打开。 我认为这是一个坏主意(出于安全原因,某些应用程序需要在超时后关闭),但这就是它的工作原理。
尽管我讨厌他们,但我认为活动巴士是前往这里的正确方法。 让每个活动都在onCreate中注册事件总线事件。 让接收方在检测到屏幕关闭(或您想要的任何其他触发器)时发送事件。 当Activity收到该事件时,请调用finish()。 为了使事情变得简单,请将此行为编码到基本活动类中,并使所有活动都从该类派生,而不是直接从活动派生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.