简体   繁体   中英

Finish All Activities from BroadCast Receiver

I want to Finish all my applications' activities from BroadCast Receiver when screen is off: First I created a static reference in my Activities as below:

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);

}

then Call is from my BroadCastreceiver Class as below:

public class SCBroadcaster extends BroadcastReceiver {
  public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
    MainActivity.ma.finish();
    }
  }
}

Do you thinks there is another better way? I tried to call the System.Exit(0) or process.killprocess(myPid) but when the screen is turned back on it will restart the whole application. Thanks.

Android isn't well set up for this. The framework's idea is that the app should never close and always be reopenable to the same spot. I think its a bad idea (some apps need to close for security reasons after a timeout), but that's how it works.

As much as I hate them, I think an event bus is the right way to go here. Have every activity register for an event bus event in onCreate. Have the Receiver send the event when it detects screen off (or whatever other trigger you want). When the Activity receives that event, have it call finish(). TO make things easier, code this behavior into a base activity class and have all your activities derive from that class instead of Activity directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM