知乎专栏 |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(getApplicationContext(), "5秒后关闭", Toast.LENGTH_SHORT).show(); final Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { //结束本界面并跳转到收派员列表的界面 finish(); } }, 5000); }
new Handler().postDelayed(new Runnable() { @Override public void run() { view.close(); } }, 10000);
程序回到桌面,例如设置WI-FI,让步在回到程序,安卓会调用 onResume()
@Override public void onResume() { super.onResume(); this.other(); }
@Override public void onBackPressed() { // code here to show dialog super.onBackPressed(); // optional depending on your needs ... }
package cn.netkiller.album; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class HotelActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hotel); TextView hotelClose = (TextView) findViewById(R.id.hotelClose); hotelClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
AndroidManifest.xml 中 activity 添加 android:launchMode="singleTask"
<activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
MainActivity 中添加 onNewIntent(Intent intent)
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (intent != null) { boolean isExit = intent.getBooleanExtra("QUIT", false); if (isExit) { this.finish(); } } }
调用 quit 方法即可正常退出主程序
public void quit(View v) { Intent intent = new Intent(this, MainActivity.class); intent.putExtra("QUIT", true); startActivity(intent); }
private boolean privacyAgreement() { String key = "PrivacyAgreement"; Cache cache = new Cache(); // cache.remove(key); boolean status = cache.isCache("PrivacyAgreement"); Log.d(TAG, "PrivacyAgreement: " + status); if (status) { return false; } setContentView(R.layout.activity_privacy_agreement); WebView webView = findViewById(R.id.webView); webView.loadUrl("http://oss.netkiller.cn/neo/docs/PrivacyAgreement.html"); Button no = findViewById(R.id.buttonNo); no.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); Button yes = findViewById(R.id.buttonYes); yes.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cache.set(key, true); Intent intent = new Intent(MainActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); // System.exit(0); // Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid()); // ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); // activityManager.restartPackage(getPackageName()); } }); return true; }