An example analysis of the operation and location of Android judgment service _android

Source: Internet
Author: User

This article illustrates the problem of whether or not the Android Judgment service is running and locating. Share to everyone for your reference. Specifically as follows:

/**
* To determine if the service is running
* 
@param context
* @param className The service name of the judgment: Package name + class name
* @return true not running on false 
   */public
Static Boolean isservicerunning (context context, String ClassName) {
  Boolean isrunning = false;
  Activitymanager Activitymanager = (activitymanager) context
    . Getsystemservice (context.activity_service);
  Get all the Services
  list<activitymanager.runningserviceinfo> services= activitymanager.getrunningservices ( Integer.max_value);
  if (Services!=null&&services.size () >0) {for
   (Activitymanager.runningserviceinfo service:services) {
    if (classname.equals (Service.service.getClassName ())) {
     isrunning=true;
     break;
    }
  }} return isrunning;
}

In the Android development, often use locationmanager.getlastknownlocation () time to obtain latitude and longitude, in the different true machine test some can get some cannot be obtained, in order to solve the compatibility of different mobile phones, please use the following code

public static Location getLocation (Locationmanager locationmanager, Locationlistener locationlistener) {
  Location Location=null;
  Location = Locationmanager.getlastknownlocation (Locationmanager.gps_provider);
  Locationmanager.requestlocationupdates (locationmanager.gps_provider, 0, 0, locationlistener);
  if (location==null) {
   location = locationmanager.getlastknownlocation (Locationmanager.network_provider);
   Locationmanager.requestlocationupdates (locationmanager.network_provider, 0, 0, locationlistener);
  return location;
}

I hope this article will help you with your Android program.

Related Article

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.