android 启动不了别的app的服务,按钮单击无法在Android App小部件中启动服务

public class Widget extends AppWidgetProvider

{

static final String TAG = "Widget";

/**

* {@inheritDoc}

*/

public void onUpdate(Context context, AppWidgetManager appWidgetManager,

int[] appWidgetIds)

{

// Create an intent to launch the service

Intent serviceIntent = new Intent(context, UpdateService.class);

// PendingIntent is required for the onClickPendingIntent that actually

// starts the service from a button click

PendingIntent pendingServiceIntent =

PendingIntent.getService(context, 0, serviceIntent, 0);

// Get the layout for the App Widget and attach a click listener to the

// button

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);

views.setOnClickPendingIntent(R.id.address_button, pendingServiceIntent);

super.onUpdate(context, appWidgetManager, appWidgetIds);

}

// To prevent any ANR timeouts, we perform the update in a service;

// really should have its own thread too

public static class UpdateService extends Service

{

static final String TAG = "UpdateService";

private LocationManager locationManager;

private Location currentLocation;

private double latitude;

private double longitude;

public void onStart(Intent intent, int startId)

{

// Get a LocationManager from the system services

locationManager =

(LocationManager) getSystemService(Context.LOCATION_SERVICE);

// Register for updates from spoofed GPS

locationManager.requestLocationUpdates("gps", 30000L, 0.0f, new LocationListener()

{

@Override

public void onLocationChanged(Location location)

{

currentLocation = location;

}

@Override

public void onProviderDisabled(String provider) {}

@Override

public void onProviderEnabled(String provider) {}

@Override

public void onStatusChanged(String provider, int status,

Bundle extras) {}

});

// Get the last known location from GPS

currentLocation =

locationManager.getLastKnownLocation("gps");

// Build the widget update

RemoteViews updateViews = buildUpdate(this);

// Push update for this widget to the home screen

ComponentName thisWidget = new ComponentName(this, Widget.class);

// AppWidgetManager updates AppWidget state; gets information about

// installed AppWidget providers and other AppWidget related state

AppWidgetManager manager = AppWidgetManager.getInstance(this);

// Updates the views based on the RemoteView returned from the

// buildUpdate method (stored in updateViews)

manager.updateAppWidget(thisWidget, updateViews);

}

public RemoteViews buildUpdate(Context context)

{

latitude = currentLocation.getLatitude();

longitude = currentLocation.getLongitude();

RemoteViews updateViews =

new RemoteViews(context.getPackageName(), R.layout.main);

updateViews.setTextViewText(R.id.latitude_text, "" + latitude);

updateViews.setTextViewText(R.id.longitude_text, "" + longitude);

return updateViews;

}

@Override

public IBinder onBind(Intent intent) {

// We don't need to bind to this service

return null;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值