以下内容来自多个开源项目的整理和自己的项目积累。
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 |
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处于前台还是后台状态