1.監(jiān)聽屏幕開閉
//用代碼創(chuàng)建一個內(nèi)部廣播接收者,監(jiān)聽開屏
private class InnerScreenReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//因為只監(jiān)聽一個動作,因此沒有必要去進行判斷了
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
//殺死進程
}
//創(chuàng)建一個activity管理器對象
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
//獲取所有運行著的進程
List<RunningAppProcessInfo> processes = am.getRunningAppProcesses();
for (RunningAppProcessInfo processInfo : processes) {
String processName = processInfo.processName;
//進程名就是應用的包名
String packageName = processName;
am.killBackgroundProcesses(packageName);
}
}
}
2.不需要權(quán)限的打電話發(fā)短信代碼
//打電話(要需要權(quán)限,因為只是跳轉(zhuǎn)到打電話的頁面去)
Uri uri = Uri.parse("tel:"+tel);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
context.startActivity(it);
//發(fā)短信(不需要權(quán)限,因為只是跳轉(zhuǎn)到打電話的頁面去)
Uri uri = Uri.parse("smsto:"+tel);
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
context.startActivity(it);
3.幾個unicode符號
在安卓的string.xml中用到的
… 代表 ... 三個點的省略號
– 代表 - 一個破折號
&#xxxx;
其中xxxx都為數(shù)字,是unicode值
此處8230對應的是unicode中的那三個點的特殊字符
4.android 橫豎屏切換
<activity android:name="MyActivity" android:configChanges="orientation|keyboardHidden">
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//加入橫屏要處理的代碼
}else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//加入豎屏要處理的代碼
}
}
5.android 獲取mac地址
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
private String getLocalMacAddress() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
}
6.android 判斷網(wǎng)絡(luò)狀態(tài)
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
private boolean getNetWorkStatus() {
boolean netSataus = false;
ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cwjManager.getActiveNetworkInfo() != null) {
netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
}
if (!netSataus) {
Builder b = new AlertDialog.Builder(this).setTitle("沒有可用的網(wǎng)絡(luò)").setMessage("是否對網(wǎng)絡(luò)進行設(shè)置?");
b.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent mIntent = new Intent("/");
ComponentName comp = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
mIntent.setComponent(comp);
mIntent.setAction("android.intent.action.VIEW");
startActivityForResult(mIntent,0);
}
}).setNeutralButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).show();
}
return netSataus;
}
7.android 根據(jù)uri獲取路徑
Uri uri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor actualimagecursor = managedQuery(uri,proj,null,null,null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
File file = new File(img_path);
8.android 開機啟動
public class StartupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startupintent = new Intent(context,StrongTracks.class);
startupintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startupintent);
}
}
<receiver android:name=".StartupReceiver">
<intent-filter>
<!-- 系統(tǒng)啟動完成后會調(diào)用 -->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
9.android 重啟
第一,root權(quán)限,這是必須的
第二,Runtime.getRuntime().exec("su -c reboot");
第三,模擬器上運行不出來,必須真機
第四,運行時會提示你是否加入列表 , 同意就好
10.android 挪動dialog的位置
Window mWindow = dialog.getWindow();
WindowManager.LayoutParams lp = mWindow.getAttributes();
lp.x = 10; //新位置X坐標
lp.y = -100; //新位置Y坐標
dialog.onWindowAttributesChanged(lp);
或者
Window =dialog.getWindow();// 得到對話框的窗口.
WindowManager.LayoutParams wl = window.getAttributes();
wl.x = x;//這兩句設(shè)置了對話框的位置.0為中間
wl.y =y;
wl.width =w;
wl.height =h;
wl.alpha =0.6f;// 這句設(shè)置了對話框的透明度
11.android 禁用home鍵盤
問題的提出:Android Home鍵系統(tǒng)負責監(jiān)聽,捕獲后系統(tǒng)自動處理。有時候,系統(tǒng)的處理往往不隨我們意,想自己處理點擊Home后的事件,那怎么辦?
問題的解決:先禁止Home鍵,再在onKeyDown里處理按鍵值,點擊Home鍵的時候就把程序關(guān)閉,或者隨你XXOO。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){ // TODO Auto-generated method stub
if(KeyEvent.KEYCODE_HOME==keyCode)
android.os.Process.killProcess(android.os.Process.myPid());
return super.onKeyDown(keyCode, event);
}
@Override
public void onAttachedToWindow(){
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
加權(quán)限禁止Home鍵
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
12.android 對話框樣式
<style name="MyDialog" parent="@android:Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/ha</item>
</style>
13.android 獲取各種窗體高度
//取得窗口屬性
getWindowManager().getDefaultDisplay().getMetrics(dm);
//窗口的寬度
int screenWidth = dm.widthPixels;
//窗口高度
int screenHeight = dm.heightPixels;
textView = (TextView)findViewById(R.id.textView01);
textView.setText("屏幕寬度: " + screenWidth + "\n屏幕高度: " + screenHeight);
二、獲取狀態(tài)欄高度
decorView是window中的最頂層view,可以從window中獲取到decorView,然后decorView有個getWindowVisibleDisplayFrame方法可以獲取到程序顯示的區(qū)域,包括標題欄,但不包括狀態(tài)欄。
于是,我們就可以算出狀態(tài)欄的高度了。
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
三、獲取標題欄高度
getWindow().findViewById(Window.ID_ANDROID_CONTENT)這個方法獲取到的view就是程序不包括標題欄的部分,然后就可以知道標題欄的高度了。
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面所求的狀態(tài)欄的高度
int titleBarHeight = contentTop - statusBarHeight
14.BitMap、Drawable、inputStream及byte[] 互轉(zhuǎn)
(1) BitMap to inputStream:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
InputStream isBm = new ByteArrayInputStream(baos .toByteArray());
(2)BitMap to byte[]:
Bitmap defaultIcon = BitmapFactory.decodeStream(in);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
(3)Drawable to byte[]:
Drawable d; // the drawable (Captain Obvious, to the rescue!!!)
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, bitmap);
byte[] bitmapdata = stream.toByteArray();
(4)byte[] to Bitmap :
Bitmap bitmap =BitmapFactory.decodeByteArray(byte[], 0,byte[].length);
15.發(fā)送指令
out = process.getOutputStream();
out.write(("am start -a android.intent.action.VIEW -n com.android.browser/com.android.browser.BrowserActivity\n").getBytes());
out.flush();
InputStream in = process.getInputStream();
BufferedReader re = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = re.readLine()) != null) {
Log.d("conio","[result]"+line);
}
16.Android獲取狀態(tài)欄和標題欄的高度
1.Android獲取狀態(tài)欄高度:
decorView是window中的最頂層view,可以從window中獲取到decorView,然后decorView有個getWindowVisibleDisplayFrame方法可以獲取到程序顯示的區(qū)域,包括標題欄,但不包括狀態(tài)欄。
于是,我們就可以算出狀態(tài)欄的高度了。
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
2.獲取標題欄高度:
getWindow().findViewById(Window.ID_ANDROID_CONTENT)這個方法獲取到的view就是程序不包括標題欄的部分,然后就可以知道標題欄的高度了。
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面所求的狀態(tài)欄的高度
int titleBarHeight = contentTop - statusBarHeight
例子代碼:
package com.cn.lhq;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
public class Main extends Activity {
ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView) this.findViewById(R.id.ImageView01);
iv.post(new Runnable() {
public void run() {
viewInited();
}
});
Log.v("test", "== ok ==");
}
private void viewInited() {
Rect rect = new Rect();
Window window = getWindow();
iv.getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT)
.getTop();
int titleBarHeight = contentViewTop - statusBarHeight;
// 測試結(jié)果:ok之后 100多 ms 才運行了
Log.v("test", "=-init-= statusBarHeight=" + statusBarHeight
+ " contentViewTop=" + contentViewTop + " titleBarHeight="
+ titleBarHeight);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>