"Kyle, take you to tamp down the application layer" Beginner's common code snippet collation (i)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

The following content comes from the consolidation of multiple open source projects and the accumulation of their own projects

      • Dial number
      • Jump to dial-up interface
      • Send SMS
      • Wake the screen and unlock it
      • Determine if the current app is in the foreground or background state
      • Determine if the current phone is in a lock screen sleep state
      • Determine if there is currently an Internet connection
      • Determine whether the current WiFi connection status
      • Install APK
      • Determine if the current device is a mobile phone
      • Gets the current device width high unit px
      • Getting the IMEI of the current device needs to be used with the isphone above
      • Get the MAC address of the current device
      • Gets the version number of the current program

Dial number
publicstaticvoidcall(Context context, String phoneNumber) {        context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)));    }
Jump to dial-up interface
publicstaticvoidcallDial(Context context, String phoneNumber) {        context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));    }
Send SMS
publicstaticvoidsendSms(Context context, String phoneNumber,            String content) {        Uri uri = Uri.parse("smsto:"                "" : phoneNumber));        new Intent(Intent.ACTION_SENDTO, uri);        intent.putExtra("sms_body""" : content);        context.startActivity(intent);    }
Wake the screen and unlock it
public static void Wakeupandunlock (context context) {Keyguardmanager km= (Keyguardmanager) Context. Getsystemservice(Context. Keyguard_service); Keyguardmanager. KeyguardlockKL = km. Newkeyguardlock("UnLock"); Unlock KL. Disablekeyguard(); Get Power Manager Object PowerManager pm= (PowerManager) context. Getsystemservice(Context. POWER_service); Get PowerManager. WakeLockObject, followed by the parameter | Indicates that two values are passed in at the same time, and the last is the tag PowerManager used in Logcat.. WakeLockWL = PM. Newwakelock(PowerManager. Acquire_causes_wakeup | PowerManager. screen_dim_wake_lock,"Bright"); Lit screen WL. Acquire(); Release WL. Release(); }

Need to add permissions

<uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Determine if the current app is in the foreground or background state
 Public Static Boolean Isapplicationbackground(FinalContext 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; }

Need to add permissions

<uses-permission     android:name="android.permission.GET_TASKS" />
Determine if the current phone is in the lock screen (sleep) state
publicstaticbooleanisSleeping(Context context) {        KeyguardManager kgMgr = (KeyguardManager) context                .getSystemService(Context.KEYGUARD_SERVICE);        boolean isSleeping = kgMgr.inKeyguardRestrictedInputMode();        return isSleeping;    }
Determine if there is currently an Internet connection
publicstaticbooleanisOnline(Context context) {        ConnectivityManager manager = (ConnectivityManager) context                .getSystemService(Activity.CONNECTIVITY_SERVICE);        NetworkInfo info = manager.getActiveNetworkInfo();        ifnull && info.isConnected()) {            returntrue;        }        returnfalse;    }
Determine whether the current WiFi connection status
    publicstaticbooleanisWifiConnected(Context context) {        ConnectivityManager connectivityManager = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo wifiNetworkInfo = connectivityManager                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);        if (wifiNetworkInfo.isConnected()) {            returntrue;        }        returnfalse;    }
Install 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);}
Determine if the current device is a mobile phone
    publicstaticbooleanisPhone(Context context) {        TelephonyManager telephony = (TelephonyManager) context                .getSystemService(Context.TELEPHONY_SERVICE);        if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {            returnfalse;        else {            returntrue;        }    }
Gets the current device width height, unit px
    @SuppressWarnings("Deprecation") Public Static int Getdevicewidth(Context context) {WindowManager manager = (windowmanager) context. Getsystemservice (Context.window_service);returnManager.getdefaultdisplay (). GetWidth (); }@SuppressWarnings("Deprecation") Public Static int Getdeviceheight(Context context) {WindowManager manager = (windowmanager) context. Getsystemservice (Context.window_service);returnManager.getdefaultdisplay (). GetHeight (); }
Gets the IMEI of the current device and needs to be used with the above 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 = Telephony. Getdeviceid();} else {deviceId = Settings. Secure. getString(Context. Getcontentresolver(), Settings. Secure. ANDROID_ID);} return DeviceId;}
Get the MAC address of the current device
    publicstaticgetMacAddress(Context context) {        String macAddress;        WifiManager wifi = (WifiManager) context                .getSystemService(Context.WIFI_SERVICE);        WifiInfo info = wifi.getConnectionInfo();        macAddress = info.getMacAddress();        if (null == macAddress) {            return"";        }        macAddress = macAddress.replace(":""");        return macAddress;    }
Gets the version number of the current program
    publicstatic String getAppVersion(Context context) {        version"0";        try {            version = context.getPackageManager().getPackageInfo(                    0).versionName;        catch (PackageManager.NameNotFoundException e) {            e.printStackTrace();        }        returnversion;    }

"Kyle, take you to tamp down the application layer" Beginner's common code snippet collation (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.