Вопрос

I'm new to android development. At the moment I'm trying to write an app widget. I've done a few widget tutorials and when I go to run them the app will run but the widget isn't showing up on the widget list to be used. Widgets that are not extensions of an app still will run as if they are an app and will not show up in the widgets list. This is happening with all the widgets iv tried to run. No errors are showing. Somebody please help its driving me mad. Thanks

<?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>

widget info xml file

<?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>

widget layout

<?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);
    }

}

widget intent broadcast receiver

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 ;



    }

}
Это было полезно?

Решение

There is a typo in your manifest. This:

<action android:name="android.appwidget.action.APPWIGET_UPDATE"/>

should be changed to this:

<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>

Notice the missing "D" in "APPWIGET_UPDATE" in the original.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top