I 화면이 꺼져있을 때 방송 수신기에서 내 모든 응용 프로그램의 활동을 마무리 할 :마침 모든 활동에서 방송 수신기
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)를 호출하려고했지만 화면이 다시 켜지면 전체 응용 프로그램이 다시 시작됩니다. 감사합니다. .
전체 'MainActivity'를 게시 할 수 있습니까? – user3793589
System.Exit (0)을 사용하면 응용 프로그램에 좋지 않은 행동을한다고 가정합니다. :). 어떤 목적으로 활동을 끝내시겠습니까? – comrade
사용자가 모든 것을 화면에 표시하지 않을 때 (휴대 전화를 옆으로 두었을 때) 잊어 버린 경우에도 응용 프로그램을 모든 방법으로 닫아야하는 목적입니다. – Albert