Android小部件列表视图项目单击不工作(小部件通过服务更新)
拜托,我有点困在这里了。我正在使用一个用于间隔重复的AlarmManager,使用后台服务(而不是使用“onUpdate”方法)更新我的小部件。一切似乎都很正常,ListView填充数据,但由于某些原因,单击ListView上的项时没有启动PendingEvent上的活动集 更新: 我认为代码可以正常工作。在其他设备上进行了测试,效果良好(摩托罗拉和HTC)。但由于某些原因,它在我的三星Galaxy S5上不起作用 下面是代码 WidgetService.javaAndroid小部件列表视图项目单击不工作(小部件通过服务更新),android,android-listview,android-pendingintent,android-appwidget,remoteview,Android,Android Listview,Android Pendingintent,Android Appwidget,Remoteview,拜托,我有点困在这里了。我正在使用一个用于间隔重复的AlarmManager,使用后台服务(而不是使用“onUpdate”方法)更新我的小部件。一切似乎都很正常,ListView填充数据,但由于某些原因,单击ListView上的项时没有启动PendingEvent上的活动集 更新: 我认为代码可以正常工作。在其他设备上进行了测试,效果良好(摩托罗拉和HTC)。但由于某些原因,它在我的三星Galaxy S5上不起作用 下面是代码 WidgetService.java public class Wi
public class WidgetService extends Service implements LocationListener, GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private LocationManager locationManager;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;
@Override
public void onCreate() {
super.onCreate();
//Initialize Google API client build and connect
buildGoogleApiClient();
//Initialize Android Location Service
locationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
//Check if Location is Enabled
if (LocationHelper.checkLocationEnabled(null, locationManager)) {
//TODO: Check Google Play Services Availability
googleApiClient.connect();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int[] widget_ids = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (widget_ids != null) {
buildUpdate(widget_ids);
}
return super.onStartCommand(intent, flags, startId);
}
private void buildUpdate(int... appwidget_ids) {
for (int appwidget_id : appwidget_ids) {
RemoteViews views = new RemoteViews(getPackageName(), R.layout.closest_banks_appwidget);
if (ClosestBanksWidget.LAST_LOCATION != null) {
Intent intent = new Intent(this, WidgetRemoteViewsService.class);
// Add the app widget ID to the intent extras.
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appwidget_id);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
/*
Intent start_activity_intent = new Intent(this, BankFinderBroadcastReceiver.class);
intent.setAction(ClosestBanksWidget.WIDGET_CLICK_INTENT_ACTION);
PendingIntent start_activity_PI = PendingIntent.getBroadcast(this, 0,
start_activity_intent, 0);
*/
Intent start_activity_intent = new Intent(this, LocationDetailActivity.class);
PendingIntent start_activity_PI = PendingIntent.getActivity(this, 0,
start_activity_intent, PendingIntent.FLAG_UPDATE_CURRENT);
String[] params = {"search", String.valueOf(ClosestBanksWidget.LAST_LOCATION.getLatitude()), String.valueOf(ClosestBanksWidget.LAST_LOCATION.getLongitude()), "", ""};
intent.putExtra("query_params", params);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
views.setRemoteAdapter(R.id.widget_location_list_view, intent);
else
views.setRemoteAdapter(appwidget_id, R.id.widget_location_list_view, intent);
views.setPendingIntentTemplate(R.id.widget_location_list_view, start_activity_PI);
}
// Push update for this widget to the home screen
//ComponentName thisWidget = new ComponentName(this, ClosestBanksWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.notifyAppWidgetViewDataChanged(appwidget_id, R.id.widget_location_list_view);
manager.updateAppWidget(appwidget_id, views);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
protected void startLocationUpdates() {
createLocationRequest();
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
protected void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setInterval(victorikoro.com.bankfinder.objects.Constants.LOCATION_UPDATE_FAST_INTERVAL * 2);
locationRequest.setFastestInterval(victorikoro.com.bankfinder.objects.Constants.LOCATION_UPDATE_FAST_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
//locationRequest.setSmallestDisplacement(Constants.MINIMUM_LOCATION_DISPLACEMENT);
}
@Override
public void onConnected(Bundle bundle) {
//Get Last Location
ClosestBanksWidget.LAST_LOCATION = LocationServices.FusedLocationApi.getLastLocation(
googleApiClient);
//Start getting periodic location updates
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
ClosestBanksWidget.LAST_LOCATION = location;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
public class WidgetRemoteViewsService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new WidgetRemoteViewsFactory(this.getApplicationContext(), intent);
}
class WidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private static final int mCount = 10;
protected List<ATMLocation> locations = new ArrayList<ATMLocation>();
private Context mContext;
private int mAppWidgetId;
private Intent intent;
protected jsqlite.Database db;
public WidgetRemoteViewsFactory(Context applicationContext, Intent intent) {
this.mContext = applicationContext;
this.intent = intent;
//Initialize DB
db = DBHelper.getDBInstance(mContext);
if (db != null) {
String[] params = intent.getStringArrayExtra("query_params");
this.locations = LocationHelper.getATMLocations(db, params).subList(0, mCount);
}
}
@Override
public void onCreate() {
}
public void onDestroy() {
if (db != null) {
try {
db.close();
} catch (jsqlite.Exception e) {
e.printStackTrace();
}
}
}
@Override
public int getCount() {
return locations.size();
}
public RemoteViews getViewAt(int position) {
ATMLocation location = locations.get(position);
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.list_item_widget);
String bank_name = LocationHelper.getFormattedBankName(location.bank, mContext);
if (location.branch.equalsIgnoreCase("true")) {
bank_name += ("(Branch)");
}
rv.setTextViewText(R.id.widget_bank_name, bank_name);
rv.setTextViewText(R.id.widget_bank_address, location.address);
rv.setTextViewText(R.id.widget_distance, LocationHelper.getFormattedDistance(location.distance));
rv.setImageViewResource(R.id.widget_bank_logo_small, LocationHelper.getBankLogoResourceID(location.bank, mContext));
// Return the remote views object.
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("latitude", location.latitude);
bundle.putString("longitude", location.longitude);
bundle.putString("id", location.id);
bundle.putString("given_id", location.given_id);
bundle.putString("bank", location.bank);
bundle.putString("email", location.email);
bundle.putString("address", location.address);
bundle.putString("tel", location.tel);
bundle.putString("tel2", location.tel2);
bundle.putString("branch", location.branch);
bundle.putString("distance", location.distance);
bundle.putString("other", location.other);
bundle.putString("machines", location.machines);
intent.putExtras(bundle);
rv.setOnClickFillInIntent(R.id.widget_list_item, intent);
return rv;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
public long getItemId(int position) {
return position;
}
public boolean hasStableIds() {
return true;
}
public void onDataSetChanged() {
if (db != null) {
locations = LocationHelper.getATMLocations(db, intent.getStringArrayExtra("query_params"));
}
}
}
}
ClosestBankWidget.java
public class ClosestBanksWidget extends AppWidgetProvider {
protected Context mContext;
public static Location LAST_LOCATION;
public static String WIDGET_CLICK_INTENT_ACTION = "victorikoro.com.bankfinder.WIDGET_START_ACTIVITY";
private PendingIntent service;
public ClosestBanksWidget() {
super();
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
mContext = context;
Intent intent = new Intent(context, WidgetService.class);
context.startService(intent);
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int N = appWidgetIds.length;
int[] widget_ids = new int[N];
int[] widget_ids_clone = new int[N];
final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
for (int i = 0; i < appWidgetIds.length; i++) {
widget_ids[i] = appWidgetIds[i];
}
final Calendar TIME = Calendar.getInstance();
TIME.set(Calendar.MINUTE, 0);
TIME.set(Calendar.SECOND, 0);
TIME.set(Calendar.MILLISECOND, 0);
final Intent i = new Intent(context, WidgetService.class);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widget_ids);
if (service == null) {
service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
}
m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), Constants.WIDGET_UPDATE_INTERVAL, service);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
}
公共类ClosestBanksWidget扩展AppWidgetProvider{
保护上下文mContext;
公共静态位置最后一个位置;
公共静态字符串小部件\单击\意图\操作=“victorikoro.com.bankfinder.WIDGET\启动\活动”;
私人吊挂服务;
公共关闭银行SWIDGET(){
超级();
}
@凌驾
公共void已启用(上下文){
super.onEnabled(上下文);
mContext=上下文;
意向意向=新意向(上下文,WidgetService.class);
上下文。startService(意图);
}
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
int N=appWidgetIds.length;
int[]widget_id=new int[N];
int[]widget_id_clone=new int[N];
最终AlarmManager m=(AlarmManager)context.getSystemService(context.ALARM\u服务);
for(int i=0;i
WidgetRemoteViewService.java
public class WidgetService extends Service implements LocationListener, GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private LocationManager locationManager;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;
@Override
public void onCreate() {
super.onCreate();
//Initialize Google API client build and connect
buildGoogleApiClient();
//Initialize Android Location Service
locationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
//Check if Location is Enabled
if (LocationHelper.checkLocationEnabled(null, locationManager)) {
//TODO: Check Google Play Services Availability
googleApiClient.connect();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int[] widget_ids = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (widget_ids != null) {
buildUpdate(widget_ids);
}
return super.onStartCommand(intent, flags, startId);
}
private void buildUpdate(int... appwidget_ids) {
for (int appwidget_id : appwidget_ids) {
RemoteViews views = new RemoteViews(getPackageName(), R.layout.closest_banks_appwidget);
if (ClosestBanksWidget.LAST_LOCATION != null) {
Intent intent = new Intent(this, WidgetRemoteViewsService.class);
// Add the app widget ID to the intent extras.
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appwidget_id);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
/*
Intent start_activity_intent = new Intent(this, BankFinderBroadcastReceiver.class);
intent.setAction(ClosestBanksWidget.WIDGET_CLICK_INTENT_ACTION);
PendingIntent start_activity_PI = PendingIntent.getBroadcast(this, 0,
start_activity_intent, 0);
*/
Intent start_activity_intent = new Intent(this, LocationDetailActivity.class);
PendingIntent start_activity_PI = PendingIntent.getActivity(this, 0,
start_activity_intent, PendingIntent.FLAG_UPDATE_CURRENT);
String[] params = {"search", String.valueOf(ClosestBanksWidget.LAST_LOCATION.getLatitude()), String.valueOf(ClosestBanksWidget.LAST_LOCATION.getLongitude()), "", ""};
intent.putExtra("query_params", params);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
views.setRemoteAdapter(R.id.widget_location_list_view, intent);
else
views.setRemoteAdapter(appwidget_id, R.id.widget_location_list_view, intent);
views.setPendingIntentTemplate(R.id.widget_location_list_view, start_activity_PI);
}
// Push update for this widget to the home screen
//ComponentName thisWidget = new ComponentName(this, ClosestBanksWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.notifyAppWidgetViewDataChanged(appwidget_id, R.id.widget_location_list_view);
manager.updateAppWidget(appwidget_id, views);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
protected void startLocationUpdates() {
createLocationRequest();
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
protected void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setInterval(victorikoro.com.bankfinder.objects.Constants.LOCATION_UPDATE_FAST_INTERVAL * 2);
locationRequest.setFastestInterval(victorikoro.com.bankfinder.objects.Constants.LOCATION_UPDATE_FAST_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
//locationRequest.setSmallestDisplacement(Constants.MINIMUM_LOCATION_DISPLACEMENT);
}
@Override
public void onConnected(Bundle bundle) {
//Get Last Location
ClosestBanksWidget.LAST_LOCATION = LocationServices.FusedLocationApi.getLastLocation(
googleApiClient);
//Start getting periodic location updates
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
ClosestBanksWidget.LAST_LOCATION = location;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
public class WidgetRemoteViewsService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new WidgetRemoteViewsFactory(this.getApplicationContext(), intent);
}
class WidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private static final int mCount = 10;
protected List<ATMLocation> locations = new ArrayList<ATMLocation>();
private Context mContext;
private int mAppWidgetId;
private Intent intent;
protected jsqlite.Database db;
public WidgetRemoteViewsFactory(Context applicationContext, Intent intent) {
this.mContext = applicationContext;
this.intent = intent;
//Initialize DB
db = DBHelper.getDBInstance(mContext);
if (db != null) {
String[] params = intent.getStringArrayExtra("query_params");
this.locations = LocationHelper.getATMLocations(db, params).subList(0, mCount);
}
}
@Override
public void onCreate() {
}
public void onDestroy() {
if (db != null) {
try {
db.close();
} catch (jsqlite.Exception e) {
e.printStackTrace();
}
}
}
@Override
public int getCount() {
return locations.size();
}
public RemoteViews getViewAt(int position) {
ATMLocation location = locations.get(position);
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.list_item_widget);
String bank_name = LocationHelper.getFormattedBankName(location.bank, mContext);
if (location.branch.equalsIgnoreCase("true")) {
bank_name += ("(Branch)");
}
rv.setTextViewText(R.id.widget_bank_name, bank_name);
rv.setTextViewText(R.id.widget_bank_address, location.address);
rv.setTextViewText(R.id.widget_distance, LocationHelper.getFormattedDistance(location.distance));
rv.setImageViewResource(R.id.widget_bank_logo_small, LocationHelper.getBankLogoResourceID(location.bank, mContext));
// Return the remote views object.
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("latitude", location.latitude);
bundle.putString("longitude", location.longitude);
bundle.putString("id", location.id);
bundle.putString("given_id", location.given_id);
bundle.putString("bank", location.bank);
bundle.putString("email", location.email);
bundle.putString("address", location.address);
bundle.putString("tel", location.tel);
bundle.putString("tel2", location.tel2);
bundle.putString("branch", location.branch);
bundle.putString("distance", location.distance);
bundle.putString("other", location.other);
bundle.putString("machines", location.machines);
intent.putExtras(bundle);
rv.setOnClickFillInIntent(R.id.widget_list_item, intent);
return rv;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
public long getItemId(int position) {
return position;
}
public boolean hasStableIds() {
return true;
}
public void onDataSetChanged() {
if (db != null) {
locations = LocationHelper.getATMLocations(db, intent.getStringArrayExtra("query_params"));
}
}
}
}
公共类WidgetRemoteViewsService扩展了RemoteViewsService{
@凌驾
公共远程视图工厂onGetViewFactory(意图){
返回新的WidgetRemoteViewsFactory(this.getApplicationContext(),intent);
}
类WidgetRemoteViewsFactory实现RemoteViewsService.RemoteViewsFactory{
专用静态最终整数mCount=10;
受保护的列表位置=新的ArrayList();
私有上下文;
私有int-mAppWidgetId;
私人意图;
受保护的jsqlite.Database数据库;
公共WidgetRemoteViewsFactory(上下文应用程序上下文,意图){
this.mContext=applicationContext;
本意=本意;
//初始化数据库
db=DBHelper.getDBInstance(mContext);
如果(db!=null){
String[]params=intent.getStringArrayExtra(“查询参数”);
this.locations=LocationHelper.getATMLocations(db,params).subList(0,mCount);
}
}
@凌驾
public void onCreate(){
}
公共空间{
如果(db!=null){
试一试{
db.close();
}catch(jsqlite.Exception e){
e、 printStackTrace();
}
}
}
@凌驾
public int getCount(){
返回位置。size();
}
公共远程视图getViewAt(内部位置){
ATMLocation location=locations.get(位置);
RemoteViews rv=新远程视图(mContext.getPackageName(),R.layout.list\u item\u小部件);
字符串bank_name=LocationHelper.getFormattedBankName(location.bank,mContext);
if(location.branch.equalsIgnoreCase(“true”)){
银行名称+=(“分行”);
}
rv.setTextViewText(R.id.widget\u bank\u name,bank\u name);
rv.setTextViewText(R.id.widget\u bank\u地址、位置、地址);
rv.setTextViewText(R.id.widget_distance,LocationHelper.getFormattedDistance(location.distance));
rv.setImageViewResource(R.id.widget_bank_logo_small,LocationHelper.getBankLogoResourceID(location.bank,mContext));
//返回远程视图对象。
意图=新意图();
Bundle=新Bundle();
bundle.putString(“纬度”,位置,纬度);
bundle.putString(“经度”,位置.经度);
bundle.putString(“id”,location.id);
bundle.putString(“给定的\u id”,位置。给定的\u id);
bundle.putString(“bank”,location.bank);
bundle.putString(“email”,location.email);
bundle.putString(“地址”,location.address);
bundle.putString(“tel”,location.tel);
bundle.putString(“tel2”,location.tel2);
bundle.putString(“branch”,location.branch);
bundle.putString(“距离”,位置,距离);
bundle.putString(“其他”,location.other);
bundle.putString(“机器”,位置.machines);
意向。额外支出(捆绑);
rv.setonClickFillinContent(R.id.widget\u列表\u项,意图);
返回rv;
}
@凌驾
公共远程视图getLoadingView(){
返回null;
}
@凌驾
public int getViewTypeCount(){
返回1;
}
公共长getItemId(int位置){
返回位置;