Android 我创建了小部件,但它们在重新启动后加载不正确
我从应用程序创建小部件。但它们在重新启动后加载不正确。实际上,我的问题是,例如,我在主屏幕上添加了三个小部件,但有时一个或两个小部件正在加载,而另一个小部件没有加载。尤克利尼约尔是指装货。请帮帮我Android 我创建了小部件,但它们在重新启动后加载不正确,android,widget,reboot,Android,Widget,Reboot,我从应用程序创建小部件。但它们在重新启动后加载不正确。实际上,我的问题是,例如,我在主屏幕上添加了三个小部件,但有时一个或两个小部件正在加载,而另一个小部件没有加载。尤克利尼约尔是指装货。请帮帮我 public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { AppWidgetManager widgetMan
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
ComponentName widgetComponent = new ComponentName(context,MyNotesWidget.class);
int[] appWidgetIds = widgetManager.getAppWidgetIds(widgetComponent);
for(int i = 0; i < appWidgetIds.length; i++){
MyNotesWidget.updateAppWidgets(context,widgetManager,appWidgetIds[i]);
}
}
}
//in my widget provider MyNotesWidget.class
public static RemoteViews updateWidgetListViews(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_notes_widget);
dataSource = new DataSource(context);
dataSource.open();
int noteId = MySharedPref.getNoteIdFromWidgetId(context,appWidgetId);
int color = dataSource.getColor(noteId);
remoteViews.setInt(R.id.widgetLinearLayout, "setBackgroundColor", color);
MyNotesWidgetActivity.widgetProcess = "receive";
Intent svcIntent = new Intent(context, WidgetViewService.class);
svcIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_ENABLED);
svcIntent.setData(Uri.fromParts("content", String.valueOf(noteId), null));
remoteViews.setRemoteAdapter(R.id.listViewWidget, svcIntent);
return remoteViews;
}
公共类MyReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
AppWidgetManager=AppWidgetManager.getInstance(上下文);
ComponentName widgetComponent=新的ComponentName(上下文,MyNotesWidget.class);
int[]appWidgetIds=widgetManager.getAppWidgetIds(widgetComponent);
for(int i=0;i
//我的小部件服务
public class WidgetViewService extends RemoteViewsService {
@Override
public RemoteViewsService.RemoteViewsFactory onGetViewFactory(Intent intent) {
return (new WidgetListProvider(this.getApplicationContext(), intent));
}
}
public class WidgetListProvider implements RemoteViewsService.RemoteViewsFactory {
private Context context;
private int appWidgetId;
private MySharedPref mySharedPref;
private int noteId;
private int widgetId;
private int size = 1;
private AppWidgetManager appWidgetManager;
public WidgetListProvider(Context context, Intent intent) {
this.appWidgetManager = AppWidgetManager.getInstance(context);
this.context = context;
this.noteId = Integer.valueOf(intent.getData().getSchemeSpecificPart());
if (0 != MyNotesWidgetActivity.widgetProcess.compareTo("create")) {
this.widgetId = intent.getIntExtra("widgetId", 0);
}
}
@Override
public int getCount() {
return size;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public RemoteViews getViewAt(int position) {
String content;
String title;
String date;
String category;
int color;
final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row);
DataSource db = new DataSource(context);
db.open();
title = db.getTitle(noteId);
content = db.getNote(noteId);
date = db.getNoteDate(noteId);
category = db.getCategory(noteId);
color = db.getColor(noteId);
remoteView.setTextViewText(R.id.title, title);
remoteView.setTextViewText(R.id.content, content);
remoteView.setTextViewText(R.id.date, date);
remoteView.setTextViewText(R.id.category, category);
if (isColorDark(color)) {
remoteView.setInt(R.id.title, "setTextColor", Color.WHITE);
remoteView.setInt(R.id.content, "setTextColor", Color.WHITE);
remoteView.setInt(R.id.date, "setTextColor", Color.WHITE);
remoteView.setInt(R.id.category, "setTextColor", Color.WHITE);
} else {
remoteView.setInt(R.id.title, "setTextColor", Color.BLACK);
remoteView.setInt(R.id.content, "setTextColor", Color.BLACK);
remoteView.setInt(R.id.date, "setTextColor", Color.BLACK);
remoteView.setInt(R.id.category, "setTextColor", Color.BLACK);
}
if (0 != MyNotesWidgetActivity.widgetProcess.compareTo("create")) {
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.content);
}
return remoteView;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void onCreate() {
}
@Override
public void onDataSetChanged() {
}
@Override
public void onDestroy() {
}
private boolean isColorDark(int color) {
double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
if (darkness < 0.2) {
return false; // It's a light color
} else {
return true; // It's a dark color
}
}
}
公共类WidgetViewService扩展了RemoteViewsService{
@凌驾
公共RemoteViewsService.RemoteViewsFactory onGetViewFactory(意图){
返回(新的WidgetListProvider(this.getApplicationContext(),intent));
}
}
公共类WidgetListProvider实现RemoteViewsService.RemoteViewsFactory{
私人语境;
私有int-appWidgetId;
私有MySharedPref MySharedPref;
私有int-noteId;
私有int-widgetId;
私有整数大小=1;
私有AppWidgetManager AppWidgetManager;
公共WidgetListProvider(上下文、意图){
this.appWidgetManager=appWidgetManager.getInstance(上下文);
this.context=上下文;
this.noteId=Integer.valueOf(intent.getData().getSchemeSpecificPart());
如果(0!=MyNotesWidgetActivity.widgetProcess.compareTo(“创建”)){
this.widgetId=intent.getIntExtra(“widgetId”,0);
}
}
@凌驾
public int getCount(){
返回大小;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共远程视图getViewAt(内部位置){
字符串内容;
字符串标题;
字符串日期;
字符串类别;
内色;
最终RemoteViews remoteView=新的remoteView(context.getPackageName(),R.layout.list_行);
DataSource db=新数据源(上下文);
db.open();
title=db.getTitle(noteId);
content=db.getNote(noteId);
日期=db.getNoteDate(noteId);
category=db.getCategory(noteId);
color=db.getColor(noteId);
setTextViewText(R.id.title,title);
setTextViewText(R.id.content,content);
setExtViewText(R.id.date,date);
setTextViewText(R.id.category,category);
if(isColorDark(颜色)){
setInt(R.id.title,“setTextColor”,Color.WHITE);
setInt(R.id.content,“setTextColor”,Color.WHITE);
setInt(R.id.date,“setTextColor”,Color.WHITE);
setInt(R.id.category,“setTextColor”,Color.WHITE);
}否则{
setInt(R.id.title,“setTextColor”,Color.BLACK);
setInt(R.id.content,“setTextColor”,Color.BLACK);
setInt(R.id.date,“setTextColor”,Color.BLACK);
setInt(R.id.category,“setTextColor”,Color.BLACK);
}
如果(0!=MyNotesWidgetActivity.widgetProcess.compareTo(“创建”)){
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId,R.id.content);
}
返回远程视图;
}
@凌驾
公共远程视图getLoadingView(){
返回null;
}
@凌驾
public int getViewTypeCount(){
返回1;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
public void onCreate(){
}
@凌驾
公共无效onDataSetChanged(){
}
@凌驾
公共空间{
}
专用布尔值isColorDark(整型颜色){
双暗度=1-(0.299*颜色。红色(颜色)+0.587*颜色。绿色(颜色)+0.114*颜色。蓝色(颜色))/255;
如果(黑暗度<0.2){
return false;//它是浅色
}否则{
return true;//这是一种深色
}
}
}
从我的清单文件:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.comradesoftware54.mynotess.boot" />
</intent-filter>
</receiver>
<receiver android:name=".MyNotesWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/my_notes_widget_info" />
</receiver>
<service
android:enabled="true"
android:exported="false"
android:name=".WidgetViewService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
我的小部件信息文件:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.comradesoftware54.mynotess.MyNotesWidgetActivity"
android:initialKeyguardLayout="@layout/my_notes_widget"
android:initialLayout="@layout/my_notes_widget"
android:minWidth="110dp"
android:minHeight="110dp"
android:previewImage="@drawable/mynotes_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen|keyguard" />
这太奇怪了,但ı在我的接收器中添加了这两行。它对我有用。:)我希望它对其他朋友有用
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT"/>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.comradesoftware54.mynotess.boot" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
祝你好运:D这太奇怪了,但ı在我的接收器里加了这两行。它对我有用。:)我希望它对其他朋友有用
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT"/>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.comradesoftware54.mynotess.boot" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
祝你好运:D