我希望在屏幕关闭时完成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);
}
然后来自我的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),但是当屏幕重新打开时,它将重新启动整个应用程序。 感谢。
答案 0 :(得分:1)
Android并没有为此做好准备。该框架的想法是应用程序永远不会关闭并始终可以重新打开到同一位置。我认为这是一个坏主意(一些应用程序需要在超时后出于安全原因关闭),但这就是它的工作原理。
尽管我讨厌他们,但我觉得活动巴士是去这里的正确方式。让所有活动在onCreate中注册参加活动巴士活动。让Receiver在检测到屏幕关闭时(或您想要的任何其他触发器)发送事件。当Activity收到该事件时,请调用finish()。为了简化操作,请将此行为编码到基本活动类中,并将所有活动从该类派生而不是直接从Activity派生。