Android开发中,常用的代码片段包括:,,1. 获取系统版本信息:
String version = android.os.Build.VERSION.RELEASE;
,2. 检查网络连接状态:ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
,3. 启动新Activity:Intent intent = new Intent(this, TargetActivity.class); startActivity(intent);
,4. 设置TextView内容:textView.setText("Hello World!");
,5. 注册广播接收器:registerReceiver(receiver, new IntentFilter("com.example.MY_ACTION"));
,6. 发送广播:sendBroadcast(new Intent("com.example.MY_ACTION"));
,7. 创建对话框:AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Title").setMessage("Message").setPositiveButton("OK", null).create().show();
,8. 读取SharedPreferences中的值:SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE); String value = preferences.getString("key", "defaultValue");
,9. 保存数据到SharedPreferences:SharedPreferences.Editor editor = preferences.edit(); editor.putString("key", "value"); editor.apply();
,10. 获取当前时间戳:long timestamp = System.currentTimeMillis();
Android实用的代码片段常用代码归纳
一、存储卡状态检查
查看是否有存储卡插入
String status = Environment.getExternalStorageState(); if (status.equals(Environment.MEDIA_MOUNTED)) { // 说明有SD卡插入 }
二、Activity透明主题设置
让某个Activity透明
在onCreate
中不设Layout,并使用以下代码:
this.setTheme(R.style.Theme_Transparent);
定义Theme_Transparent
(注意transparent_bg
是一副透明的图片):
<style name="Theme.Transparent"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@drawable/transparent_bg</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:backgroundDimEnabled">false</item> </style>
三、屏幕元素句柄获取
在屏幕元素中设置句柄
TextView msgTextView = (TextView) findViewById(R.id.msg); msgTextView.setText(R.string.push_me);
四、发送短信与彩信
发送短信
String body = "this is mms demo"; Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null)); mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body); mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true); mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true); startActivity(mmsintent);
发送彩信
StringBuilder sb = new StringBuilder(); sb.append("file://"); sb.append(fd.getAbsoluteFile()); Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mmsto", number, null)); intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject); intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body); intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString()); intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode); intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent); startActivity(intent);
五、发送邮件
发送邮件
mime = "img/jpg"; shareIntent.setDataAndType(Uri.fromFile(fd), mime); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fd)); shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject); shareIntent.putExtra(Intent.EXTRA_TEXT, body);
六、广播接收器注册
7. 注册一个BroadcastReceiver
registerReceiver(mMasterResetReciever, new IntentFilter("oms.action.MASTERRESET")); private BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() { public void onReceive(Context context, Intent intent){ String action = intent.getAction(); if("oms.action.MASTERRESET".equals(action)){ RecoverDefaultConfig(); } } }
七、数据表监听器定义
8. 定义ContentObserver,监听某个数据表
private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI); private class DownloadsChangeObserver extends ContentObserver { public DownloadsChangeObserver(Uri uri) { super(new Handler()); } @Override public void onChange(boolean selfChange) {} }
八、获取手机UA
获取手机UA
public String getUserAgent() { String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null); return user_agent; }
九、清空手机上Cookie
清空手机上Cookie
CookieSyncManager.createInstance(getApplicationContext()); CookieManager.getInstance().removeAllCookie();
十、建立GPRS连接
建立GPRS连接
private boolean openDataConnection() { DataConnection conn = DataConnection.getInstance(); if (connectMode == 0) { ret = conn.openConnection(mContext, "cmwap", "cmwap", "cmwap"); } else { ret = conn.openConnection(mContext, "c"); } return ret; }
到此,以上就是小编对于“Android实用的代码片段常用代码归纳”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。