1、在标题栏中加入进度条:
//明确进度条
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
setProgressBarIndeterminateVisibility(true);
// 明确进度条
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
setProgress(5000);
2、设置全屏
//设置全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 去除标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
3、锁定屏幕方向
<activity android:name=”.EX01″
android:label=”@string/app_name”
android:screenOrientation=”portrait”>
</activity>
4、创建快捷方式
OPhone上的方法
![android小技巧 - 心灵之窗 - 我的博客我做主 android小技巧 - 心灵之窗 - 我的博客我做主](http://img.ph.126.net/8v_AH2qI1QipFA1JFztGBA==/1534320098050042816.gif)
Android上的实现方法
Java代码
- private static final String ACTION_INSTALL_SHORTCUT = “com.android.launcher.action.INSTALL_SHORTCUT”;
Java代码
- Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);
- shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
- getString(R.string.app_name));
- shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
- Intent intent = new Intent();
- intent.setComponent(new ComponentName(this.getPackageName(),
- “.Splash”));
- shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
- shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- Intent.ShortcutIconResource.fromContext(this,
- R.drawable.icon));
- sendBroadcast(shortcutIntent);
Uses permission
Xml代码
- <uses-permission android:name=“com.android.launcher.permission.INSTALL_SHORTCUT”/>
删除快捷方式试了很久没测试成功过,不知那位同学贡献一下代码?
5、判断网络是否可用
函数用于判断网络是否可用
Java代码
- /*
- *@return boolean return true if the application can access the internet
- */
- private boolean haveInternet(){
- NetworkInfo info=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo();
- if(info==null || !info.isConnected()){
- return false;
- }
- if(info.isRoaming()){
- //here is the roaming option you can change it if you want to disable internet while roaming, just return false
- return true;
- }
- return true;
- }
另一种方法
Java代码
- public boolean isNetworkAvailable() {
- Context context = getApplicationContext();
- ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- if (connectivity == null) {
- boitealerte(this.getString(R.string.alert),“getSystemService rend null”);
- } else {
- NetworkInfo[] info = connectivity.getAllNetworkInfo();
- if (info != null) {
- for (int i = 0; i < info.length; i++) {
- if (info[i].getState() == NetworkInfo.State.CONNECTED) {
- return true;
- }
- }
- }
- }
- return false;
- }
6、在启动时自动启动一个应用程序:
1、AndroidManifest.xml
Xml代码
- <receiver android:enabled=“true” android:name=“.BootUpReceiver”
- android:permission=“android.permission.RECEIVE_BOOT_COMPLETED”>
- <intent-filter>
- <action android:name=“android.intent.action.BOOT_COMPLETED” />
- <category android:name=“android.intent.category.DEFAULT” />
- </intent-filter>
- </receiver>
2、permission
Java代码
- <uses-permission android:name=“android.permission.RECEIVE_BOOT_COMPLETED” />
3、BroadcastReceiver实现
Java代码
- public class BootUpReceiver extends BroadcastReceiver{
- @Override
- public void onReceive(Context context, Intent intent) {
- Intent i = new Intent(context, MyActivity.class);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(i);
- }
- }
7、
Android Kill App
两种方法
第一种:
Java代码
- android.os.Process.killProcess(android.os.Process.myPid())
- System.exit(0);
第二种:
Java代码
- ActivityManager.restartPackage(packageName);
8、
转换Bitmap to Drawable
Java代码
- BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;
- Drawable drawable = (Drawable)bitmapDrawable;
- Bitmap bitmap = new Bitmap (…);
- Drawable drawable = new BitmapDrawable(bitmap);
转换Drawable to Bitmap
Java代码
- Drawable d = ImagesList.get(0);
- Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
9、获取当前设备的IP地址
Java代码
- WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
- WifiInfo wifiInfo = wifiManager.getConnectionInfo();
- int ipAddress = wifiInfo.getIpAddress();
- String ip = intToIp(ipAddress);
- public String intToIp(int i) {
- return ((i >> 24 ) & 0xFF ) + “.” +
- ((i >> 16 ) & 0xFF) + “.” +
- ((i >> 8 ) & 0xFF) + “.” +
- ( i & 0xFF) ;
- }
10、程序控制屏幕变亮:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);
11、一个图片透明:
Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);buffer.eraseColor(Color.TRANSPARENT);
12、过滤特定文本
Filter filter = myAdapter.getFilter();
filter.filter(mySearchText);
13、拖放控制
Button1.setOnTouchListener(new Button.OnTouchListener() {
int index[] = new int[]{0,0};
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
int x = (int)event.getRawX();
int y = (int)event.getRawY();
switch(action)
{
case MotionEvent.ACTION_DOWN:
Log.i(“taohaibing”, “move down”);
index[0] = (int)event.getX();
index[1] = y-v.getTop();
break;
case MotionEvent.ACTION_MOVE:
Log.i(“taohaibing”, “ACTION_MOVE”);
v.layout(x-index[0], y-index[1], x+v.getWidth()-index[0],y-index[1]+v.getHeight());
v.postInvalidate();
break;
case MotionEvent.ACTION_UP:
Log.i(“taohaibing”, “ACTION_UP”);
break;
}
return false;
}
});