我正在尝试用widget创建android应用程序,但在nexus 5设备上发现了奇怪的行为(我有两个 - 一个用棒棒糖,一个用棉花糖)。如果我调用AppWidgetManager.notifyAppWidgetViewDataChanged,小部件的ListView内容将在其他设备(Nexus 3,ASUS K010)上刷新,但在这两个设备上没有任何反应。找不到任何关于此的信息。
所以这是我的代码:MainActivity.java。
public class MainActivity extends AppCompatActivity {
public static final String PREFS_TAG = "StringList";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View v = findViewById(R.id.fill);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fillWidget();
}
});
v= findViewById(R.id.clear);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearWidget();
}
});
}
private void fillWidget(){
SharedPreferences preferences = getSharedPreferences(WidgetRemoteService.class.getSimpleName(), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Set<String> strings = new HashSet<>();
strings.add("111111111111111");
strings.add("222222222222222");
strings.add("333333333333333");
strings.add("444444444444444");
strings.add("555555555555555");
strings.add("666666666666666");
strings.add("777777777777777");
strings.add("888888888888888");
strings.add("999999999999999");
editor.putStringSet(PREFS_TAG, strings);
editor.commit();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
ComponentName componentName = new ComponentName(getApplicationContext(),WidgetProvider.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listView);
}
private void clearWidget(){
SharedPreferences preferences = getSharedPreferences(WidgetRemoteService.class.getSimpleName(), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.remove(PREFS_TAG);
editor.commit();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
ComponentName componentName = new ComponentName(getApplicationContext(),WidgetProvider.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listView);
}
}
WidgetProvider.java
public class WidgetProvider extends AppWidgetProvider {
private static final String TAG = WidgetProvider.class.getSimpleName();
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int appWidgetId : appWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId,createRemoteViews(context, appWidgetId));
}
}
public static RemoteViews createRemoteViews(Context context, int appWidgetId){
Log.e(TAG, "Creating remote views");
Intent intent = new Intent(context, WidgetRemoteService.class);
// Add the app widget ID to the intent extras.
Log.e(TAG, "Creating remote views for widget #"+appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews result = new RemoteViews(WidgetProvider.class.getPackage().getName(),R.layout.widget_layout);
result.setRemoteAdapter(R.id.listView,intent);
return result;
}
}
WidgetRemoteService.java
public class WidgetRemoteService extends RemoteViewsService {
private static final String TAG = WidgetRemoteService.class.getSimpleName();
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
int appWidgetID = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
return new WidgetViewsFactory(getApplicationContext(), appWidgetID);
}
public static class WidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory{
private Context context;
private String[] strings;
private int appWidgetId;
public WidgetViewsFactory(Context context, int appWidgetId){
Log.e(TAG, "Factory created");
this.context = context;
this.appWidgetId = appWidgetId;
}
@Override
public void onCreate() {
}
@Override
public void onDataSetChanged() {
Log.e(TAG, "Dataset changed");
SharedPreferences preferences = context.getSharedPreferences(WidgetRemoteService.class.getSimpleName(), Context.MODE_PRIVATE);
Set<String> stringSet = preferences.getStringSet(MainActivity.PREFS_TAG, new HashSet<String>());
strings = stringSet.toArray(new String[]{});
}
@Override
public void onDestroy() {
}
@Override
public int getCount() {
if (strings==null)
return 0;
else
return strings.length;
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews result = new RemoteViews(getClass().getPackage().getName(),R.layout.widget_item);
result.setTextViewText(R.id.textTitle,"This is title");
result.setTextViewText(R.id.textContent,strings[position]);
return result;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
}
}
这里我尝试从MainActivity更新小部件,但不知何故它在我的nexus 5设备上不起作用 - 根据日志onDataSetChanged()从未在此设备上调用,但在其他设备上它可以工作。也许我在导游中误解了一些东西,它不应该工作,并且仅仅因为一些bug而在其他设备上工作,这在以后的Android版本中得到修复?
答案 0 :(得分:0)
尝试添加
android:permission="android.permission.BIND_REMOTEVIEWS"
RemoteViewsService
AndroidManifest.xml
,我今天也遇到了这个问题。
答案 1 :(得分:0)
我设置了一个断点,想知道为什么没有wildfly
被调用。事实证明,它被调用了,但是只有在我后台启动应用程序并且该小部件可见后,它才能被调用!