2016-07-04 59 views
0

我要完成從廣播接收我的所有應用程序的活動時,屏幕關閉: 首先,我創造了我的活動如下靜態參考:完成所有活動從廣播接收器

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

你可以發佈你的整個'MainActivity'? – user3793589

+0

我想使用System.Exit(0)是不適合應用程序:)。爲了什麼目的你想完成活動? – comrade

+0

即使用戶忘記了(當他將手機放在一邊時),應用程序必須以各種方式關閉,因此在關閉所有內容時都要關閉。 – Albert

回答

1

Android沒有很好地爲此設置。該框架的想法是,應用程序不應該關閉,並始終可以重新打開到同一地點。我認爲它是一個壞主意(有些應用需要在超時後出於安全原因關閉),但這就是它的工作原理。

儘管我討厭他們,但我認爲一個活動巴士是去這裏的正確方法。讓每個活動都在onCreate中註冊一個事件總線事件。讓接收器在檢測到屏幕關閉時(或其他任何您想要的觸發器)發送事件。當活動收到該事件時,讓它調用finish()。爲了使事情更簡單,將此行爲編碼到基本活動類中,並讓所有活動都從該類派生而不是直接派生。