标签:返回键 ons 程序 mem tag 实现 任务 list 技巧总结
0、Android彻底退出引用程序的方法
1) Dalvik VM的本地方法
android.os.Process.killProcess(android.os.Process.myPid()); // 获取PID System.exit(0); // 常规java的标准退出法,返回值为0代表正常退出
2)任务管理器方法
首先要说明该方法执行在Android 1.5 API Level为3以上才干够。同一时候须要权限
// 获取ActivityManager实例 ActivityManager am = (ActivityManager)getSystemService (Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName());系统会将,该包下的 ,所有进程。服务,所有杀掉。就能够杀干净了,要注意加上权限声明:
<!-- 权限声明 --> <uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>
3) 依据Activity的声明周期
我们知道Android的窗体类提供了历史栈。我们能够通过stack的原理来巧妙的实现,这里我们在A窗体打开B窗体时在Intent中直接增加标志 Intent.FLAG_ACTIVITY_CLEAR_TOP,这样开启B时将会清除该进程空间的全部Activity。
在A窗体中使用以下的代码调用B窗体
Intent intent = new Intent(); intent.setClass(Android123.this, CWJ.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG设置 startActivity(intent);
接下来在B窗体中须要退出时直接使用finish方法就可以所有退出。
4)使用SingleTask模式
将主Activity(记为MainActivity)设置为SingleTask模式,须要退出程序时,能够先跳转到MainActivity。然后调用finish函数退出程序。
5)将全部Activity注冊一个接收finish消息的广播:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); IntentFilter filter = new IntentFilter(); filter.addAction("finish"); registerReceiver(mFinishReceiver, filter); …… } private BroadcastReceiver mFinishReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if ("finish".equals(intent.getAction())) { finish(); } } };
6)自己定义一个Activity栈,道理同上,只是利用一个单例模式的Activity栈来管理全部Activity,并加入提供了可以退出全部Activity的方法。代码例如以下:
public class ScreenManager { private static Stack<Activity> activityStack; private static ScreenManager instance; private ScreenManager() { } public static ScreenManager getScreenManager() { if (instance == null) { instance = new ScreenManager(); } return instance; } // 退出栈顶Activity public void popActivity(Activity activity) { if (activity != null) { activity.finish(); activityStack.remove(activity); activity = null; } } // 获得当前栈顶Activity public Activity currentActivity() { Activity activity = activityStack.lastElement(); return activity; } // 将当前Activity推入栈中 public void pushActivity(Activity activity) { if (activityStack == null) { activityStack = new Stack<Activity>(); } activityStack.add(activity); } // 退出栈中全部Activity public void popAllActivityExceptOne(Class cls) { while (true) { Activity activity = currentActivity(); if (activity == null) { break; } if (activity.getClass().equals(cls)) { break; } popActivity(activity); } } }
//监听返回键,点击两次退出 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { onBackCodePressed(); } return false; } private int mPressedTime = 0; private void onBackCodePressed() { // 表示第一次点击 if(mPressedTime == 0){ Toast.makeText(this, "连续点击退出程序 ", Toast.LENGTH_SHORT).show(); ++mPressedTime; new Thread(){ @Override public void run() { try { sleep(2000); } catch (Exception e) { Log.e(TAG, e.toString()); } finally { mPressedTime = 0; } } }; } else { android.os.Process.killProcess(android.os.Process.myPid()); } }
<?xml version="1.0"encoding="utf-8"?> <!-- 绘制边框 --> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:right="0dp"> <shape android:shape="rectangle"> <solid android:color="@color/white"/> </shape> </item> <item android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp"> <shape android:shape="rectangle"> <solid android:color="@color/orchid"/> </shape> </item> </layer-list>
TextUtils.isEmpty(str);
TextUtils.isEmpty源代码:
public static boolean isEmpty(CharSequence str) { if (str == null || str.length() == 0) return true; else return false; }
3、推断横屏竖屏并进行切换:
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){ setContentView(R.layout.横屏); } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { setContentView(R.layout.竖屏); }
public <T> T $(int viewID) { return (T) findViewById(viewID); }
TextView a; a = $(R.id.a_id);
SimpleDateFormat formatter = new SimpleDateFormat( "yyyy年MM月dd日 HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis());// 获取当前时间 String str = formatter.format(curDate);
String label = DateUtils.formatDateTime( getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
private final static int[] li_SecPosValue = { 1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590 }; private final static String[] lc_FirstLetter = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z" }; // 获得每一个string的首字母 /** * 取得给定汉字的首字母,即声母 * @param chinese 给定的汉字 * @return 给定汉字的声母 */ public String getFirstLetter(String chinese) { if (chinese == null || chinese.trim().length() == 0) { return ""; } chinese = this.conversionStr(chinese, "GB2312", "ISO8859-1"); if (chinese.length() > 1) { // 推断是不是汉字 int li_SectorCode = (int) chinese.charAt(0); // 汉字区码 int li_PositionCode = (int) chinese.charAt(1); // 汉字位码 li_SectorCode = li_SectorCode - 160; li_PositionCode = li_PositionCode - 160; int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 汉字区位码 if (li_SecPosCode > 1600 && li_SecPosCode < 5590) { for (int i = 0; i < 23; i++) { if (li_SecPosCode >= li_SecPosValue[i] && li_SecPosCode < li_SecPosValue[i + 1]) { chinese = lc_FirstLetter[i]; break; } } } else { // 非汉字字符,如图形符号或ASCII码 chinese = this.conversionStr(chinese, "ISO8859-1", "GB2312"); chinese = chinese.substring(0, 1); } } return chinese; } /** * 字符串编码转换 * @param str 要转换编码的字符串 * @param charsetName 原来的编码 * @param toCharsetName 转换后的编码 * @return 经过编码转换后的字符串 */ private String conversionStr(String str, String charsetName,String toCharsetName) { try { str = new String(str.getBytes(charsetName), toCharsetName); } catch (UnsupportedEncodingException ex) { System.out.println("字符串编码转换异常:" + ex.getMessage()); } return str; }
private ArrayList<String> mItems; Collections.sort(mItems, Collator.getInstance(java.util.Locale.CHINA)); // 排序
//是否连接WIFI public static boolean isWifiConnected(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return wifiNetworkInfo.isConnected(); } /**** 打开WIFI***/ public static boolean openWifi(Context context){ return opclWifi(context, true); } /**** 关闭WIFI***/ public static boolean closeWifi(Context context){ return opclWifi(context, false); } /**** 打开关闭WIFI****/ private static boolean opclWifi(Context context, boolean type){ boolean result = true; // 获取WIFIManager实例 WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (wifiManager.isWifiEnabled() ^ type) { // 打开关闭WIFI result = wifiManager.setWifiEnabled(type); } return result; }
加入权限:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
9、int数据转化成十六进制。二进制。八进制。无符号数字符串:
public static void printInt(int i) { System.out.println(Integer.toBinaryString(i)); // 二进制输出 System.out.println(Integer.toHexString(i)); // 十六进制输出 System.out.println(Integer.toOctalString(i)); // 八进制输出 System.out.println(Integer.toUnsignedString(i)); // 转化成无符号数输出 System.out.println(Integer.toString(i)); // 转化成String输出 }測试:
System.out.println("======== 输出正数 ========="); int i = 100; printInt(i); System.out.println("======== 输出负 ========="); i = -100; printInt(i);输出结果:
======== 输出正数 ========= 1100100 64 144 100 100 ======== 输出负 ========= 11111111111111111111111110011100 ffffff9c 37777777634 4294967196 -100
标签:返回键 ons 程序 mem tag 实现 任务 list 技巧总结
原文地址:http://www.cnblogs.com/tlnshuju/p/7087206.html