문제

I have a simple application and I want a homescreen widget (an Image) that runs the app from one of the activities, not exactly the Main Activity. I have no errors in the compilation, running the app or installing it, but the widget isn't installed neither in a tablet or a phone I am using for testing. So... this is what I am doing.

widget_info.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="146dp"
    android:minWidth="146dp"
    android:configure=".MyWidgetProvider"
    android:widgetCategory="home_screen|keyguard"
    android:previewImage="@drawable/ic_launcher" 
    android:updatePeriodMillis="3000" >    
</appwidget-provider>

widget_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:orientation="horizontal"
    android:layout_width="146dp"
    android:layout_height="146dp"
    android:layout_margin="8dip"
    android:background="@drawable/ic_launcher" >    
</LinearLayout> 

MyWidgetProvider.java

package com.project;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;

public class MyWidgetProvider extends AppWidgetProvider {


  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {


      RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);

      ComponentName myWidget = new ComponentName(context,MyWidgetProvider.class);
      AppWidgetManager manager = AppWidgetManager.getInstance(context);
      manager.updateAppWidget(myWidget, remoteViews);


  }
} 

And this is the receiver inside Application in the AndroidManifest.xml

<receiver android:name="Detente" >
            <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                 <action android:name="android.appwidget.action.ACTION_WIDGET_RECEIVER"/>
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
 </receiver> 

Do I need to add any permission or something like that or maybe this "android:installLocation="preferExternal"" is causing conflict?

(I'm running the tests on a Lenovo Tab whit ICS and a LG 3D-Max with GB )

I need this for tomorrow, please help. :D

도움이 되었습니까?

해결책 2

It was as simple as moving the application to the device... it was being installed to the SD because of this

 android:installLocation="preferExternal"

Keep in mind that the widgets can only be installed if the app is in the internal memory.

다른 팁

The problem is likely that you have not declared your AppWidgetProvider correctly.

As per the App Widgets guide,

The element requires the android:name attribute, which specifies the AppWidgetProvider used by the App Widget.

Your AppWidgetProvider is called MyWidgetProvider, but your manifest says <receiver android:name="Detente" >. Changing this to <receiver android:name="MyWidgetProvider" > may solve your problem.

I also do not see an ACTION_WIDGET_RECEIVER in the AppWidgetManager documentation. Where did that come from? The documentation indicates that only APPWIDGET_UPDATE is required.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top