Android Widget not working
I have no idea what's going on. My OnReceive method is never called. It's probably something in the manifest.
Widget Provider:
package com.net_refresh;
import android.app.PendingIntent;
import android.appwidget.AppWidgetProvider;
import android.content.Intent;
import android.content.Context;
import android.provider.Settings;
import android.view.View;
import android.widget.RemoteViews;
import android.appwidget.AppWidgetManager;
public class NetRefreshWidget extends AppWidgetProvider
{
private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appwidgetmanager, int[] appWidgetIds)
{
final int length = appWidgetIds.length;
for (int i=0; i<length; i++)
{
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, NetRefreshWidget.class);
intent.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_initial_layout);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
appwidgetmanager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds)
{
}
@Override
public void onEnabled(Context context)
{
}
@Override
public void onDisabled(Context context)
{
}
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER))
{
//Top secret code here.
}
}
Manifest:
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.net_refresh" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="7"/&g开发者_Python百科t;
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".NetRefresh">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".NetRefreshWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/>
</receiver>
</application>
</manifest>
Widget info XML:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_initial_layout"
>
</appwidget-provider>
Widget layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_root"
android:layout_width="72dp"
android:orientation="vertical"
android:layout_height="72dp"
>
<Button android:text="Button" android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="wrap_content"></Button>
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/icon" android:clickable="true" android:adjustViewBounds="false" android:id="@+id/img_refresh" android:layout_toRightOf="@+id/button1" android:layout_alignTop="@+id/button1" android:layout_alignBottom="@+id/button1">
</ImageView>
</RelativeLayout>
The code says nothing without you to point the compiler's errors & warnings.
Anyway you can wrap your code into .apk and install it by yourself into your device, BUT this is the curve way, because everyone I know are using the eclipse and SDK tools.
Have you tried using this: <uses-permission android:name="android.permission.INTERNET" />
. It may solve it.
I figured it out a while back. I don't remember what I changed exactly but I at least added this line:
<action android:name="android.appwidget.action.ACTION_WIDGET_RECEIVER"/>
To the manifest, next to:
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
Very simple solution, now that I have an understanding of how broadcasts work.
精彩评论