36个Android开发常⽤经典代码⼤全
本⽂汇集36个Android开发常⽤经典代码⽚段,包括、、唤醒屏幕并解锁、是否有⽹络连接、动态显⽰或者是隐藏软键盘等,希望对您有所帮助。
//36个Android开发常⽤代码⽚段
//
public static void call(Context context, String phoneNumber) {
context.startActivity( new Intent(Intent.ACTION_CALL, Uri.parse( "tel:" + phoneNumber)));
}
//跳转⾄拨号界⾯
public static void callDial(Context context, String phoneNumber) {
context.startActivity( new Intent(Intent.ACTION_DIAL, Uri.parse( "tel:" + phoneNumber)));
}
//
public static void sendSms(Context context, String phoneNumber,
String content) {
Uri uri = Uri.parse( "smsto:"
+ (TextUtils.isEmpty(phoneNumber) ? "" : phoneNumber));
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra( "sms_body" , TextUtils.isEmpty(content) ? "" : content);
context.startActivity(intent);
}
//唤醒屏幕并解锁
public static void wakeUpAndUnlock(Context context){
KeyguardManager km= (KeyguardManager) SystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock( "unLock" );
//解锁
kl.disableKeyguard();
//获取电源管理器对象
PowerManager pm=(PowerManager) SystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后⾯的参数|表⽰同时传⼊两个值,最后的是LogCat⾥⽤的Tag
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright" ); //点亮屏幕
wl.acquire();
//释放
}
//需要添加权限
<uses-permission android:name= "android.permission.WAKE_LOCK" />
<uses-permission android:name= "android.permission.DISABLE_KEYGUARD" />
//判断当前App处于前台还是后台状态
public static boolean isApplicationBackground( final Context context) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
@SuppressWarnings ( "deprecation" )
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks( 1 ); if (!tasks.isEmpty()) {
ComponentName topActivity = ( 0 ).topActivity;
if (!PackageName().PackageName())) { return true ;
}
}
return false ;
}
//需要添加权限
<uses-permission
android:name= "android.permission.GET_TASKS" />
//判断当前⼿机是否处于锁屏(睡眠)状态
public static boolean isSleeping(Context context) {
KeyguardManager kgMgr = (KeyguardManager) context
.getSystemService(Context.KEYGUARD_SERVICE);
boolean isSleeping = kgMgr.inKeyguardRestrictedInputMode();
return isSleeping;
}
//判断当前是否有⽹络连接
public static boolean isOnline(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo info = ActiveNetworkInfo();
if (info != null && info.isConnected()) {
return true ;
}
return false ;
}
//判断当前是否是WIFI连接状态
public static boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetworkInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetworkInfo.isConnected()) {
return true ;
}
return false ;
}
//安装APK
public static void installApk(Context context, File file) {
Intent intent = new Intent();
intent.setAction( "android.intent.action.VIEW" );
intent.addCategory( "android.intent.category.DEFAULT" );
intent.setType( "application/vnd.android.package-archive" );
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive" );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
//判断当前设备是否为⼿机
public static boolean isPhone(Context context) {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (PhoneType() == TelephonyManager.PHONE_TYPE_NONE) { return false ;
} else {
return true ;
}
}
//获取当前设备宽⾼,单位px
@SuppressWarnings ( "deprecation" )
public static int getDeviceWidth(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DefaultDisplay().getWidth();
}
@SuppressWarnings ( "deprecation" )
public static int getDeviceHeight(Context context) {
WindowManager manager = (WindowManager) context
.
getSystemService(Context.WINDOW_SERVICE);
DefaultDisplay().getHeight();
}
//获取当前设备的IMEI,需要与上⾯的isPhone()⼀起使⽤
@TargetApi (Build.VERSION_CODES.CUPCAKE)
public static String getDeviceIMEI(Context context) {
String deviceId;
if (isPhone(context)) {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
deviceId = DeviceId();
} else {
deviceId = ContentResolver(), Settings.Secure.ANDROID_ID);
}
return deviceId;
}
//获取当前设备的MAC地址
public static String getMacAddress(Context context) {
String macAddress;
WifiManager wifi = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = ConnectionInfo();
macAddress = MacAddress();
if ( null == macAddress) {
return "" ;
}
macAddress = place( ":" , "" );
return macAddress;
}
//获取当前程序的版本号
public static String getAppVersion(Context context) {
String version = "0" ;
try {
version = PackageManager().getPackageInfo(
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return version;
}
//收集设备信息,⽤于信息统计分析
public static Properties collectDeviceInfo(Context context) {
Properties mDeviceCrashInfo = new Properties();
try {
PackageManager pm = PackageManager();
PackageInfo pi = pm.PackageName(), PackageManager.GET_ACTIVITIES);
if (pi != null ) {
mDeviceCrashInfo.put(VERSION_NAME,
pi.versionName == null ? "not set" : pi.versionName);
mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode);
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Error while collect package info" , e);
}
Field[] fields = Build. class .getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible( true );
mDeviceCrashInfo.Name(), ( null ));
} catch (Exception e) {
Log.e(TAG, "Error while collect crash info" , e);
}
}
return mDeviceCrashInfo;
}
public static String collectDeviceInfoStr(Context context) {
Properties prop = collectDeviceInfo(context);
Set deviceInfos = prop.keySet();
StringBuilder deviceInfoStr = new StringBuilder( "{\n" );
for (Iterator iter = deviceInfos.iterator(); iter.hasNext();) {
Object item = ();
deviceInfoStr.append( "\t\t\t" + item + ":" + (item)
+ ", \n" );
}
deviceInfoStr.append( "}" );
String();
}
/
/是否有SD卡
public static boolean haveSDCard() {
return android.ExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
//动态隐藏软键盘
安卓课程设计源代码@TargetApi (Build.VERSION_CODES.CUPCAKE)
public static void hideSoftInput(Activity activity) {
View view = Window().peekDecorView();
if (view != null ) {
InputMethodManager inputmanger = (InputMethodManager) activity .getSystemService(Context.INPUT_METHOD_SERVICE);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论