JavaWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JavaWorld@TW » Java ME、Google Android 平台與 JavaCard 討論區 » Android  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 有人有寫過LBS的Widgets嗎
noobking0





發文: 67
積分: 0
於 2010-02-09 17:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
最近有在練習寫Widgets

目前是想寫一個LBS的Widgets 但是卻無法開啟GPS
以下是我在抓取GPS用的的程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.WidgetTest;
 
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
 
public class GetLocation {
  Location location = null;
  
  public Location getLocation(Context context){
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    getLocationPrivider(locationManager);
    while(location == null){
      if(AppConfig.DEBUG){
        Log.i("location check", "check");
      }
    }
    
    return location;
  }
  
  
  
  public void getLocationPrivider(LocationManager locationManager) {
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
      try {
 
        Criteria criteria = new Criteria();
        String strLocationPrivider = "";
        strLocationPrivider = locationManager.getBestProvider(criteria, true);
        locationManager.getLastKnownLocation(strLocationPrivider);
        locationManager.requestLocationUpdates(strLocationPrivider, 2000, 10, locationListener);
        
        
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  
  private LocationListener locationListener = new LocationListener() {
 
    @Override
    public void onLocationChanged(Location arg0) {
      location = arg0;
    }
 
    @Override
    public void onProviderDisabled(String provider) {
      // TODO Auto-generated method stub
    }
 
    @Override
    public void onProviderEnabled(String provider) {
      // TODO Auto-generated method stub
    }
 
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
      // TODO Auto-generated method stub
    }
  };
}
 


在我非GPS上的成是這段程式碼是有作用的
但不知為何到Widget程式就變得沒作用

在manifest 也確認有
1
2
3
<uses-permission android:name ="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_CROSE_LOCATION"/>


不知有人是否有一樣的經驗?


vote up 0 vote down
reply to postreply to post
一位願意學習的新人
作者 Re:有人有寫過LBS的Widgets嗎 [Re:noobking0]
samlu_128





發文: 162
積分: 2
於 2010-02-09 22:23 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
建議你再仔細研讀 widget 的文件及了解 widget 的生命週期。
widget 在 onReceive() 後,就結束了。你得起個 service ,並在其中收 GPS 訊號。


vote up 0 vote down
reply to postreply to post
blog: ysl 的程式天堂
web1: The #1 world clock for Android
web2: 美食, 旅遊, 住宿景點分享網站
程式讓我在我的想像天堂裡,盡情創造
作者 Re:有人有寫過LBS的Widgets嗎 [Re:noobking0]
noobking0





發文: 67
積分: 0
於 2010-02-10 17:33 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我把抓GPS的程式碼放在Severs也是一模一樣
以下是我部分的程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
  public void onStart(Intent intent, int startId) {
    
    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    RemoteViews updateViews = null;
    
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    getLocationPrivider(locationManager);
    updateViews = buildUpdate(this);
 
            
    // Push update for this widget to the home screen
    ComponentName thisWidget = new ComponentName(this, VponWidget.class);
    manager.updateAppWidget(thisWidget, updateViews);
  }


不知道有沒有有看過用Severs開GPS的範例 我想那會對我很有幫助


vote up 0 vote down
reply to postreply to post
一位願意學習的新人
作者 Re:有人有寫過LBS的Widgets嗎 [Re:noobking0]
noobking0





發文: 67
積分: 0
於 2010-02-11 10:23 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
今天在戶外測試結果就是之前的寫法可以抓到GPS

但是不知為何手機上方沒有GPS的ICON 出現

之前都會出現一個GPS的閃爍ICON阿...............


vote up 0 vote down
reply to postreply to post
一位願意學習的新人

» JavaWorld@TW »  Java ME、Google Android 平台與 JavaCard 討論區 » Android

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JavaWorld@TW


Powered by Powerful JuteForum® Version Jute 1.5.8
Copyright© 2002-2003 Rainman Zhu,Zua,Netboy,Scott. All Rights Reserved.