• Android GPS 开发
  • 发布于 2个月前
  • 255 热度
    0 评论
  • 李宏荣
  • 0 粉丝 39 篇博客
  •   

AndroidManifest.xml

1
2
3
<uses-permission android:name="android.permission.RESTART_PACKAGES" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>

main.java

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.mrsoft.mrgps.app.mr;
 
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
 
public class main extends Activity {
    private TextView tv;
    private LocationManager lm;
    private Criteria criteria;
    private Location location;
    private final static int MENU_ABOUT = Menu.FIRST;  
    private final static int MENU_EXIT = Menu.FIRST+1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        tv = (TextView)findViewById(R.id.tv);
         
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
         
        if (!lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
        {
            Toast.makeText(this"GPS已关闭,请手动开启GPS后再试!", Toast.LENGTH_SHORT).show();
            return;
        }
        else
        {
            Toast.makeText(this"GPS定位中...", Toast.LENGTH_SHORT).show();
        }
         
        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);    // 设置精确度
        criteria.setAltitudeRequired(true);                // 设置请求海拔
        criteria.setBearingRequired(true);                // 设置请求方位
        criteria.setCostAllowed(true);                    // 设置允许运营商收费
        criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
         
        String provider = lm.getBestProvider(criteria, true);
        location = lm.getLastKnownLocation(provider);
        newLocalGPS(location);
        // 监听1秒一次 忽略位置变化
        lm.requestLocationUpdates(provider, 1*10000new locationListener());
    }
     
    class locationListener implements LocationListener
    {
 
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            newLocalGPS(location);
        }
 
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            newLocalGPS(null);
        }
 
        @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
             
        }
         
    }
     
    private void newLocalGPS(Location location)
    {
        if (location!=null)
        {
            double latitude = location.getLatitude(); //精度
            double longitude = location.getLongitude(); // 纬度
            double speed = location.getSpeed();    // 速度
            double altitude = location.getAltitude();    // 海拔
            double bearing = location.getBearing();    // 方位
            tv.setText("精度"+latitude+'\n'+
                           "纬度"+longitude+'\n'+
                           "速度"+speed+"m/s"+'\n'+
                           "海拔"+altitude+"m"+'\n'+
                           "方位"+bearing+'\n');
        }
        else
        {
            // 未获取地理信息位置
            tv.setText("地理信息位置未知或正在获取地理信息位置中...");
        }
    }
     
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        menu.add(0, MENU_ABOUT, 1"关于");  
        menu.add(0, MENU_EXIT, 2"退出");
        return super.onCreateOptionsMenu(menu);
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId())
        {
        case MENU_ABOUT:
             AlertDialog.Builder bd = new Builder(main.this);  
                bd.setMessage("mrGPS.apk\n版本:1.0\n作者:mrandexe");  
                bd.setTitle("关于");  
                bd.setPositiveButton("确认"new OnClickListener(){  
                    @Override  
                    public void onClick(DialogInterface arg0, int arg1) {  
                        // TODO Auto-generated method stub  
                        arg0.dismiss();  
                    }  
                });  
                bd.create().show(); 
            break;
        case MENU_EXIT:
            exit();
            break;
        }
        return super.onOptionsItemSelected(item);
    }
 
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        // TODO Auto-generated method stub  
        if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)  
        {  
            exit();  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    
     
    private void exit()  
    {  
         AlertDialog.Builder builder = new Builder(main.this);  
         builder.setMessage("确认退出吗?");  
         builder.setTitle("提示");  
         builder.setPositiveButton("确认"new OnClickListener(){  
             @Override  
             public void onClick(DialogInterface arg0, int arg1) {  
                 // TODO Auto-generated method stub  
                 arg0.dismiss();
                 ActivityManager actMgr = (ActivityManager)getSystemService(ACTIVITY_SERVICE);  
                 actMgr.restartPackage(getPackageName());
             }  
         });  
         builder.setNegativeButton("取消"new OnClickListener(){  
 
             @Override  
             public void onClick(DialogInterface dialog, int which) {  
                 // TODO Auto-generated method stub  
                 dialog.dismiss();  
             }  
         });  
         builder.create().show();
          
    }
}
main.xml

1
2
3
4