AIDL Study Notes 2 get geographical location from Service

Source: Internet
Author: User

This program uses the Baidu map API, so you need to import the corresponding package. : Http://www.bkjia.com/uploadfile/2011/1219/20111219032516866.zip

Baidu map API is used because google is not very stable. After testing, Baidu map api is still relatively stable.

 

There are many examples of how to import data to the Internet.


1. AIDL File

ForMainActivity


Package com. android. aidl;
 
Interface ForMainActivity {
Void longitude maction (double latitude, double longpolling );
}
ForGetLocationService


Package com. android. aidl;
 
Import com. android. aidl. ForMainActivity;
Interface ForGetLocationService {
Void registCallback (ForMainActivity forMainActivity );
}
2. Service


Package com. android. service;
 
Import com. baidu. mapapi. BMapManager;
Import com. baidu. mapapi. LocationListener;
Import com. android. aidl. ForGetLocationService;
Import com. android. aidl. ForMainActivity;
Import com. android. util. Debugger;
 
Import android. app. Service;
Import android. content. Intent;
Import android. location. Location;
Import android. OS. IBinder;
Import android. OS. RemoteException;
 
Public class GetLocationService extends Service {

Private ForMainActivity callBack;

Private LocationListener locationListener = null;
Private BMapManager mapManager;
 
@ Override
Public void onCreate (){
Debugger. debug ("GetLocation Service onCreate ().");
Init ();
Super. onCreate ();
}
 
@ Override
Public void onDestroy (){
Debugger. debug ("GetLocation Service onDestroy ().");
If (mapManager! = Null ){
MapManager. destroy ();
MapManager = null;
}
Super. onDestroy ();
}
 
@ Override
Public void onRebind (Intent intent ){
Debugger. debug ("GetLocation Service onRebind ().");
Super. onRebind (intent );
}
 
@ Override
Public void onStart (Intent intent, int startId ){
Debugger. debug ("GetLocation Service onStart ().");
If (mapManager! = Null ){
MapManager. getLocationManager (). requestLocationUpdates (locationListener );
MapManager. start ();
}
Super. onStart (intent, startId );
}
 
@ Override
Public boolean onUnbind (Intent intent ){
Debugger. debug ("GetLocation Service onUnbind ().");
Return super. onUnbind (intent );
}
 
@ Override
Public IBinder onBind (Intent intent ){
Debugger. debug ("GetLocation Service onBind ().");
If (mapManager! = Null ){
MapManager. getLocationManager (). requestLocationUpdates (locationListener );
MapManager. start ();
}
Return mBinder;
}

Private void init (){
// Initialize MapActivity
MapManager = new BMapManager (getApplication ());
// The first parameter of the init method must be filled with the applied API Key.
MapManager. init ("285b450ebab2a92293e85502150ada7f03c777c4", null );
MapManager. start ();

LocationListener = new LocationListener (){

@ Override
Public void onLocationChanged (Location location ){
Try {
CallBack. callBack maction (location. getLatitude (), location. getlongdistance ());
} Catch (RemoteException e ){
E. printStackTrace ();
}
}
};
}

Private final ForGetLocationService. Stub mBinder = new ForGetLocationService. Stub (){

@ Override
Public void registCallback (ForMainActivity forMainActivity)
Throws RemoteException {
Debugger. debug ("callback value assignment ");
CallBack = forMainActivity;
}
};
}


3. Activity


Private ForMainActivity. Stub callBack = new ForMainActivity. Stub (){

@ Override
Public void longitude maction (double latitude, double longpolling)
Throws RemoteException {
Debugger. debug ("MainActivity callBack into maction excuted .");
Debugger. debug ("From service: latitude->" + latitude + "," + "longpolling->" + longpolling );

// Here, Handler is used to send the obtained information, and UI operations cannot be performed here
Bundle bundle = new Bundle ();
Bundle. putDouble (LATITUDE, latitude );
Bundle. putDouble (longpolling, longpolling );
Message message = new Message ();
Message. what = MSG_LOCATION_GOT;
Message. setData (bundle );
Handler. sendMessage (message );
}
};

ForGetLocationService forGetLocationService;
Private ServiceConnection connection = new ServiceConnection (){

@ Override
Public void onServiceDisconnected (ComponentName name ){
Debugger. debug ("MainActivity onServiceDisconnected .");
ForGetLocationService = null;
}

@ Override
Public void onServiceConnected (ComponentName name, IBinder service ){
Debugger. debug ("MainActivity onServiceConnected .");
ForGetLocationService = ForGetLocationService. Stub. asInterface (service );
Try {
ForGetLocationService. registCallback (callBack );
} Catch (RemoteException e ){
E. printStackTrace ();
}
}
};

Add the following code to the onCreate () function:


Bundle bundle = new Bundle ();
Intent intent = new Intent (MainActivity. this, GetLocationService. class );
Intent. putExtras (bundle );
BindService (intent, connection, Context. BIND_AUTO_CREATE );
StartService (intent );
4. Manifest File


<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. android"
Android: versionCode = "1"
Android: versionName = "@ string/version_name">
 
<Uses-sdk android: minSdkVersion = "7"/>
 
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"> </uses-permission>
 
<Application
Android: icon = "@ drawable/qq"
Android: label = "@ string/app_name">
<Activity
Android: configChanges = "orientation | keyboardHidden | navigation"
Android: label = "@ string/app_name"
Android: name = ". activities. MainActivity"
Android: screenOrientation = "portrait"
Android: theme = "@ android: style/Theme. Light">
</Activity>
<Service android: name = ". service. GetLocationService" android: process = ": remote"> </service>
 
</Application>
 
</Manifest>

From the column qiaoning13256

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.