Android代码片段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android代码片段相关的知识,希望对你有一定的参考价值。
1.拨打电话
1
2
3
|
public static void call(Context context, String phoneNumber) {
context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)));
}
|
2.跳转至拨号界面
1
2
3
|
public static void callDial(Context context, String phoneNumber) {
context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
}
|
3.发送短信
1
2
3
4
5
6
7
8
|
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);
}
|
4.唤醒屏幕并解锁
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static void wakeUpAndUnlock(Context context){
KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
//解锁
kl.disableKeyguard();
//获取电源管理器对象
PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
//点亮屏幕
wl.acquire();
//释放
wl.release();
}
|
5.需要添加权限
1
2
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
|
6.判断当前App处于前台还是后台状态
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
|
7.需要添加权限
1
2
|
<uses-permission
android:name="android.permission.GET_TASKS" />
|
8.判断当前手机是否处于锁屏(睡眠)状态
1
2
3
4
5
6
|
public static boolean isSleeping(Context context) {
KeyguardManager kgMgr = (KeyguardManager) context
.getSystemService(Context.KEYGUARD_SERVICE);
boolean isSleeping = kgMgr.inKeyguardRestrictedInputMode();
return isSleeping;
}
|
9.判断当前是否有网络连接
1
2
3
4
5
6
7
8
9
|
public static boolean isOnline(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
return true;
}
return false;
}
|
10.判断当前是否是WIFI连接状态
1
2
3
4
5
6
7
8
9
10
|
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;
}
|
11.安装APK
1
2
3
4
5
6
7
8
9
10
|
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);
}
|
12.判断当前设备是否为手机
1
2
3
4
5
6
7
8
9
|
public static boolean isPhone(Context context) {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
return false;
} else {
return true;
}
}
|
13.获取当前设备宽高,单位px
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@SuppressWarnings("deprecation")
public static int getDeviceWidth(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
return manager.getDefaultDisplay().getWidth();
}
@SuppressWarnings("deprecation")
public static int getDeviceHeight(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
return manager.getDefaultDisplay().getHeight();
}
|
14.获取当前设备的IMEI,需要与上面的isPhone()一起使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static String getDeviceIMEI(Context context) {
String deviceId;
if (isPhone(context)) {
TelephonyManager telephony = (TelephonyManager) c 以上是关于Android代码片段的主要内容,如果未能解决你的问题,请参考以下文章 |