Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

110.3. 生命周期

110.3.1. 定时关闭

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

110.3.2. 恢复触发

程序回到桌面,例如设置WI-FI,让步在回到程序,安卓会调用 onResume()

		
    @Override
    public void onResume() {
        super.onResume();
        this.other();
    }		
		
			

110.3.3. 返回触发

		
    @Override
    public void onBackPressed() {
        // code here to show dialog
        super.onBackPressed();  // optional depending on your needs
        ...
    }		
		
			

110.3.4. Activity 关闭

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

    }
}		
		
			

110.3.4.1. 退出 App

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

110.3.5. 重启自己

		
    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;


    }