先看下效果吧!
[img]http://dl.iteye.com/upload/attachment/477644/5c95dcd3-ef28-32d1-a97a-fb01c5f7fdeb.png[/img]
MyAppWidgetProvider .java
public class MyAppWidgetProvider extends AppWidgetProvider{
private Timer timer;
private int[] appWidgetIds;
private AppWidgetManager appWidgetManager;
private Context context;
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds){
super.onUpdate(context, appWidgetManager, appWidgetIds);
this.appWidgetIds = appWidgetIds;
this.appWidgetManager = appWidgetManager;
this.context = context;
Intent intent = new Intent(context,MyLocation.class);
context.startService(intent);
// views.setTextViewText(R.id.tvMsg, "text");
//appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
service:
public class MyLocation extends Service{
public ArrayList<String> locationArr = new ArrayList<String>();
LocationManager lm;
LocationListener ll = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Location l = lm.getLastKnownLocation(provider);
updtateView(l);
}
@Override
public void onProviderDisabled(String provider) {
updtateView(null);
}
@Override
public void onLocationChanged(Location location) {
updtateView(location);
}
};
@Override
public void onCreate() {
super.onCreate();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//设置查询条件
String bestProvider = lm.getBestProvider(getCriteria(), true);
//获取位置
Location l = lm.getLastKnownLocation(bestProvider);
updtateView(l);
//设置监听器
lm.requestLocationUpdates(bestProvider,5000,8,ll);
}
//编写查询条件的方法
public Criteria getCriteria(){
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);//设置查询精度
criteria.setSpeedRequired(false);//设置是否要求速度
criteria.setCostAllowed(false);//设置是否产生费用
criteria.setBearingRequired(false);//设置是否得到方向
criteria.setAltitudeRequired(false);//设置是否得到海拔高度
criteria.setPowerRequirement(Criteria.POWER_LOW);//设置运行的电池级别
return criteria;
}
public void updtateView(Location newLocation){
if (newLocation!=null) {
locationArr.add(String.valueOf(newLocation.getLatitude()));//添加纬度
locationArr.add(String.valueOf(newLocation.getLongitude()));//添加经度
}else {
locationArr.add("没有获取您的位置");
}
}
public ArrayList<String> getMessage(){
if (locationArr!=null) {
return locationArr;
}else {
return null;
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
RemoteViews views = new RemoteViews(this.getPackageName(),
R.layout.firstappwidget);
String content = "您的位置:"+"\n"+"纬度:"+getMessage().get(0)+
"\n"+"经度:"+getMessage().get(1);
views.setTextViewText(R.id.tvMsg,content);
ComponentName thisWidget = new ComponentName(this,MyAppWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, views);
}
public String getTime(){
Time myTime = new Time();
myTime.setToNow();
return myTime.format("%H:%M:%S");
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
系统配置文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sunny.app"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".MyAppWidgetProvider">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_provider" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
<service android:name="sunny.app.MyLocation"/>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOACTION"/>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="200dp"
android:minHeight="100dp"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/firstappwidget"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tvMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="21dp"
android:textColor="#FFFFFF"
android:background="@drawable/background_shape"/>
</LinearLayout>
[img]http://dl.iteye.com/upload/attachment/477644/5c95dcd3-ef28-32d1-a97a-fb01c5f7fdeb.png[/img]
MyAppWidgetProvider .java
public class MyAppWidgetProvider extends AppWidgetProvider{
private Timer timer;
private int[] appWidgetIds;
private AppWidgetManager appWidgetManager;
private Context context;
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds){
super.onUpdate(context, appWidgetManager, appWidgetIds);
this.appWidgetIds = appWidgetIds;
this.appWidgetManager = appWidgetManager;
this.context = context;
Intent intent = new Intent(context,MyLocation.class);
context.startService(intent);
// views.setTextViewText(R.id.tvMsg, "text");
//appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
service:
public class MyLocation extends Service{
public ArrayList<String> locationArr = new ArrayList<String>();
LocationManager lm;
LocationListener ll = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Location l = lm.getLastKnownLocation(provider);
updtateView(l);
}
@Override
public void onProviderDisabled(String provider) {
updtateView(null);
}
@Override
public void onLocationChanged(Location location) {
updtateView(location);
}
};
@Override
public void onCreate() {
super.onCreate();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//设置查询条件
String bestProvider = lm.getBestProvider(getCriteria(), true);
//获取位置
Location l = lm.getLastKnownLocation(bestProvider);
updtateView(l);
//设置监听器
lm.requestLocationUpdates(bestProvider,5000,8,ll);
}
//编写查询条件的方法
public Criteria getCriteria(){
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);//设置查询精度
criteria.setSpeedRequired(false);//设置是否要求速度
criteria.setCostAllowed(false);//设置是否产生费用
criteria.setBearingRequired(false);//设置是否得到方向
criteria.setAltitudeRequired(false);//设置是否得到海拔高度
criteria.setPowerRequirement(Criteria.POWER_LOW);//设置运行的电池级别
return criteria;
}
public void updtateView(Location newLocation){
if (newLocation!=null) {
locationArr.add(String.valueOf(newLocation.getLatitude()));//添加纬度
locationArr.add(String.valueOf(newLocation.getLongitude()));//添加经度
}else {
locationArr.add("没有获取您的位置");
}
}
public ArrayList<String> getMessage(){
if (locationArr!=null) {
return locationArr;
}else {
return null;
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
RemoteViews views = new RemoteViews(this.getPackageName(),
R.layout.firstappwidget);
String content = "您的位置:"+"\n"+"纬度:"+getMessage().get(0)+
"\n"+"经度:"+getMessage().get(1);
views.setTextViewText(R.id.tvMsg,content);
ComponentName thisWidget = new ComponentName(this,MyAppWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, views);
}
public String getTime(){
Time myTime = new Time();
myTime.setToNow();
return myTime.format("%H:%M:%S");
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
系统配置文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sunny.app"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".MyAppWidgetProvider">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_provider" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
<service android:name="sunny.app.MyLocation"/>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOACTION"/>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="200dp"
android:minHeight="100dp"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/firstappwidget"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tvMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="21dp"
android:textColor="#FFFFFF"
android:background="@drawable/background_shape"/>
</LinearLayout>