我是Android开发的新手。目前我正在尝试编写应用小部件。我已经完成了一些小部件教程,当我去运行它们时,应用程序将运行,但小部件未显示在要使用的小部件列表上。不是应用程序扩展的窗口小部件仍将像应用程序一样运行,并且不会显示在窗口小部件列表中。这种情况发生在iv试图运行的所有小部件上。没有错误显示。有人请帮助它让我发疯。感谢
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.somethinginspiring"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.somethinginspiring.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- declaring widget -->
<receiver android:name="AppWidgetProv"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<intent-filter >
<action
android:name="android.appwidget.action.APPWIGET_UPDATE"/>
</intent-filter>
<!-- declaring the widget_info -->
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info"/>
</receiver>
<!-- declaring broadcaster -->
<receiver
android:name="WidgetIntentReceiver"
android:label="WidgetBroadcastReceiver" >
<intent-filter>
<action android:name="android.appwidget.intent.action.CHANGE_PICTURE"/>
</intent-filter>
<!-- declaring meta-data -->
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info"/>
</receiver>
</application>
</manifest>
小部件信息xml文件
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="72dp"
android:minWidth="300dp"
android:widgetCategory="home_screen"
android:minResizeHeight="72dp"
android:minResizeWidth="300dp"
android:updatePeriodMillis="2000000">
<!-- android:updatePerMillis="3000000" -->
</appwidget-provider>
小部件布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/widget_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/image_view_text"
android:clickable="true"
android:src="@drawable/j_1" >
</ImageView>
</LinearLayout>
app widget provider
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class AppWidgetProv extends AppWidgetProvider {
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteView.setOnClickPendingIntent(R.id.widget_image_view, buildButtonPendingIntent(context));
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public PendingIntent buildButtonPendingIntent(Context context) {
Intent changePicture = new Intent();
changePicture.setAction("android.widget.intent.action.CHANGE_PICTURE");
return PendingIntent.getBroadcast(context, 0, changePicture, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteView){
ComponentName widget = new ComponentName(context, AppWidgetProv.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(widget, remoteView);
}
}
小部件意图广播接收器
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class WidgetIntentReceiver extends BroadcastReceiver {
private static int currentImage = 0;
int[] images = {R.drawable.j_1};
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.appwidget.intent.action.CHANGE_PICTURE"));
RemoteViews remote = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
remote.setImageViewResource(R.id.widget_image_view, getImageToSet());
}
private int getImageToSet(){
currentImage++;
return currentImage = currentImage % images.length ;
}
}
答案 0 :(得分:0)
你的清单中有一个拼写错误。这样:
<action android:name="android.appwidget.action.APPWIGET_UPDATE"/>
应改为:
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
请注意原件中“APPWIGET_UPDATE”中缺少“D”。