First look at the effect:
The requirement is very simple. Add the weather query button to the menu and click it to display real-time weather.
Preparations:
1. Download Huawei SDK; http://imax.vmall.com/nj-campus/universityEpDown/toDownPage
2. Apply for an application to obtain the appid and appkey, which will be used later.
The simple idea is to first obtain the longitude and latitude of the current position through the network or GPS, and then query the temperature through the SDK to obtain the result.
The procedure is as follows:
1. Create a project
Drag the jar package in the SDK to the libs folder in the project.
2. The main class code is as follows:
Package COM. empty. weatherreport; import COM. empty. weatherreport. weatherutil. scell; import COM. empty. weatherreport. weatherutil. sparts; import COM. IMAX. vmall. SDK. android. common. adapter. servicecallback; import COM. IMAX. vmall. SDK. android. entry. capabilityservice; import COM. IMAX. vmall. SDK. android. entry. commonservice; import COM. IMAX. vmall. SDK. android. huawei. weather. weatherservice; import android. location. locat Ion; import android. location. locationmanager; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. app. activity; import android. app. alertdialog; import android. app. progressdialog; import android. content. context; import android. util. log; import android. view. menu; import android. view. menuitem; import android. widget. textview; import android. widget. Toast; public class weatheractivity extends activity {private myhandler; private progressdialog mprogressdialog; private location mlocation; private Boolean sdkstatus; // tool to get weather/*** commonservice */private commonservice Cs;/*** weatherservice */private weatherservice weather; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcon Tentview (R. layout. activity_weather); sdkstatus = false; myhandler = new myhandler (); // initialize the service interface instance weather = capabilityservice. getweatherserviceinstance (); // instantiate commonservicecs = commonservice. getinstance (); initsdk ();} private void initsdk () {// Application ID. Go to the IMAX platform to register and apply for string appid = "******"; // application keystring appkey = "******"; // call the authentication interface through commonservice. Before calling other capabilities, ensure that the authentication Initialization is successful. CS. init (weatheractivity. this, appid, appkey, new serviceca Llback () {public void onerror (string arg0) {// todo auto-generated method stub // Set message MSG = new message (); MSG. what = 2; MSG. OBJ = "SDK initialize failed! "; Myhandler. sendmessage (MSG);} public void oncomplete (string arg0) {// todo auto-generated method stub // Set message MSG = new message (); MSG = new message (); MSG. what = 2; MSG. OBJ = "SDK initialize success! "; Sdkstatus = true; myhandler. sendmessage (MSG) ;}}) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. activity_weather, menu); Return true ;}@ override public Boolean onmenuitemselected (INT featureid, menuitem item) {// todo auto-generated method stub if (item. getitemid () = R. id. menu_settings) toast. maketext (getapplicationcontext (), "ha", toast. length_short ). show (); If (item. getitemid () = R. id. menu_weather) {If (sdkstatus) {/** a waiting box is displayed */mprogressdialog = new progressdialog (this); mprogressdialog. setmessage ("waiting... "); mprogressdialog. setprogressstyle (progressdialog. style_spinner); mprogressdialog. show (); weatherthread M = new weatherthread (); New thread (m ). start ();} elsetoast. maketext (getapplicationcontext (), "SDK not installed", toast. length_short ). show ();} return Super. onmenuitemselected (featureid, item);}/** display result */private void showresult (string s) {string TMP [] = S. split ("\" "); For (INT I = 0; I <TMP. length; I ++) log. I ("tmp" + I, TMP [I]); New alertdialog. builder (this ). settitle ("Weather "). setmessage ("latitude:" + mlocation. getlatitude () + "\ n longpolling:" + mlocation. getlongpolling () + "\ ntmperature:" + TMP [21]). show ();} class myhandler extends handler {public myhandler () {} public myhandler (low.l) {super (l) ;}// The subclass must override this method, take over data @ override public void handlemessage (Message MSG) {// todo auto-generated method stub log. D ("myhandler", "handlemessage ...... ");/** display result */switch (MSG. what) {Case 1: log. I ("error", "case1"); mprogressdialog. dismiss (); showresult (string) MSG. OBJ); break; Case 2: Toast. maketext (getapplicationcontext (), (string) MSG. OBJ, toast. length_short ). show (); break; default:;} super. handlemessage (MSG); // UI can be updated here} class weatherthread implements runnable {public void run () {final message MSG = new message (); MSG. what = 1; try {mlocation = getlocation (weatheractivity. this); weather. getweather (double. tostring (mlocation. getlongpolling (), double. tostring (mlocation. getlatitude (), new servicecallback () {public void onerror (string arg0) {// API call error response log. I ("error", "getweather error:" + arg0); // sets the message MSG. OBJ = arg0;/** close dialog box */myhandler. sendmessage (MSG); // send a message to handler, update UI} public void oncomplete (string arg0) {// response to log after successful API call. I ("complete", "getweather complete:" + arg0); // sets the message MSG. OBJ = arg0;/** close dialog box */myhandler. sendmessage (MSG); // send a message to handler, update UI});} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace () ;}}// get the location by GPS or WiFi public location getlocation (context) {locationmanager locman = (locationmanager) context. getsystemservice (context. location_service); location = locman. getlastknownlocation (locationmanager. gps_provider); If (location = NULL) {location = locman. getlastknownlocation (locationmanager. network_provider) ;}return location ;}}
3. add permissions to the manifest File
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.INTERNET" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
Enhanced Edition
It is not enough to use text expression alone. The following figure shows the weather conditions.
The idea is to update the imageview Based on the returned result field.
First, add some weather pictures to RES/drwable:
Add an imageview space on the main interface.
Modify the private void showresult (string s) function.
/** Display result */private void showresult (string s) {string TMP [] = S. split ("\" "); For (INT I = 0; I <TMP. length; I ++) log. I ("tmp" + I, TMP [I]); New alertdialog. builder (this ). settitle ("Weather "). setmessage ("latitude:" + mlocation. getlatitude () + "\ n longpolling:" + mlocation. getlongpolling () + "\ ntmperature:" + TMP [21] + S ). show (); int weathercode = integer. parseint (TMP [13]); Switch (weathercode) {Case 26: Case 27: Case 28: Case 29: Case 30: Case 44: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. cloudy); break; case 20: Case 22: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. foggy); break; Case 21: Case 19: Case 23: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. dusty); break; Case 24: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. windy); break; Case 36: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. hot); break; Case 13: Case 14: Case 15: Case 16: Case 43: Case 46: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. snow); break; Case 3: Case 4: Case 37: Case 38: Case 39: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. thunderstorms); break; Case 11: Case 12: Case 40: Case 45: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. showers); break; Case 5: Case 6: Case 7: Case 18: Case 35: Case 41: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. mixrainandsnow); break; Case 32: Case 34: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. sunny); break; case 8: Case 9: Case 10: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. freezerain); break; Case 17: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. hail); break; Case 1: Case 2: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. tornado); break; Case 25: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. cold); break; Case 31: Case 33: mimageview. setimagedrawable (this. getresources (). getdrawable (R. drawable. clear); break; default :;}}
The effect is as follows: