android软件完全退出方式
来源:互联网 发布:网络犯罪举报网站 编辑:程序博客网 时间:2024/05/17 02:08
如题:这里介绍一种安卓软件完全退出方式:
启动界面的activity(StartActivity.java):
package com.tody.exitdemo;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.content.Intent;import android.view.KeyEvent;import android.view.Menu;/** * 启动界面 * * @author tody.yang * */public class StartActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_start);//加载布局}/** * 自定义Handler对象 */Handler mHandler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case 0:Intent mIntent = new Intent();//实例化一个Intent对象mIntent.setClass(StartActivity.this, MainActivity.class);//设置所跳转的类的类名startActivity(mIntent);//启动跳转界面至MainActivity界面finish();//关掉当前界面break;default:break;}};};/** * 重写onResume方法 */@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();mHandler.sendMessageDelayed(mHandler.obtainMessage(0), 500);//延时500毫秒发送一个消息}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}启动界面的布局文件(activity_start.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".StartActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="我是启动界面" android:textSize="20sp" /></RelativeLayout>
主界面activity(MainActivity.java):
package com.tody.exitdemo;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.os.Handler;import android.view.KeyEvent;import android.widget.Toast;/** * 主界面 * * @author tody.yang * */public class MainActivity extends Activity {private boolean exitFlag = false;//退出的标志位private final int EXIT_CODE = 1;//消息的处理标志位private Toast mToast;//Toast类对象,该类是用来弹出警示框的。private ExitBroadcastReceiver mExitBroadcastReceiver;//广播接收器@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);IntentFilter mIntentFilter = new IntentFilter(ExitRun.EXIT_ACTION);//实例化IntentFilter对象,同时添加actionmExitBroadcastReceiver = new ExitBroadcastReceiver();//实例化自定义的广播接收器registerReceiver(mExitBroadcastReceiver, mIntentFilter);//动态注册广播}/** * 自定义的广播接收类 * * @author tody.yang * */public class ExitBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubif (ExitRun.EXIT_ACTION.equals(intent.getAction())) {System.out.println("2222222222222222222222");MainActivity.this.finish();//关掉MainActivity界面}}}/** * */Handler mHandler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case EXIT_CODE:exitFlag = false;break;default:break;}};};@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubswitch (keyCode) {case KeyEvent.KEYCODE_BACK:if (exitFlag) {if (mToast != null) {mToast.cancel();mToast = null;}ExitRun.exitApplication(this);} else {exitFlag = true;mToast = Toast.makeText(MainActivity.this, "再按一次退出",Toast.LENGTH_SHORT);mToast.show();mHandler.sendMessageDelayed(mHandler.obtainMessage(EXIT_CODE),3000);}return true;default:return super.onKeyDown(keyCode, event);}}}
主界面布局文件(activity_main.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".StartActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="我是主界面" android:textSize="20sp" /></RelativeLayout>
自定义退出线程类(ExitRun.java):
package com.tody.exitdemo;import android.content.Context;import android.content.Intent;import android.os.Handler;/** * 自定义退出的线程 * * @author tody.yang * */public class ExitRun implements Runnable {public static final String EXIT_ACTION = "tody.exit.action";//所传广播的action变量./** * 自定义退出应用的方法 * @param context 上下文对象(因为需要发送广播,而发送广播的方法需要上下文对象) */public static void exitApplication(Context context) {Intent mIntent = new Intent(EXIT_ACTION);//实例化一个intent,同时添加actioncontext.sendBroadcast(mIntent);//发送广播Handler mHandler = new Handler();//实例化一个HandlemHandler.postDelayed(new ExitRun(), 500);//Handle延时调用进程}@Overridepublic void run() {//进程的run方法// TODO Auto-generated method stubSystem.out.println("你妹啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");android.os.Process.killProcess(android.os.Process.myPid());//杀死进程.}}
- android软件完全退出方式
- Android App完全退出方式
- android应用完全退出的多种方式
- Android退出应用程序,完全退出!
- android完全退出应用程序的三种方式
- android完全退出应用程序的三种方式 .
- android完全退出应用程序的三种方式 .
- android完全退出应用程序的三种方式
- android完全退出程序的几种方式
- Android 程序完全退出的几种方式
- android 完全退出程序
- Android完全退出应用程序
- android 完全退出程序
- Android完全退出应用程序
- android 完全退出应用程序
- android 完全退出应用程序
- android完全退出程序
- android 完全退出应用程序
- 你的名字在存储器中是用什么代码表示的
- 如何让Lync 2013支持显示脱机状态
- 常用的三角公式
- 高仿遇见客户端
- iOS - 获取设备语言:
- android软件完全退出方式
- Android软件开发之盘点所有Dialog对话框大合集(一)
- 用python写爬虫
- 怎么关闭xp远程管理和默认共享
- 如何训练思维的深度和缜密度?
- cpu 执行程序的过程(微机原理)
- Objective C - 中三种集合NSArray、NSSet、NSDictionary用法:
- Android 创建项目或者运行项目期间R文件丢失的问题
- 成都软件园实习那些话