I'm trying to make a very simple widget there. It seems to be somehow unregistered WidgetProvider, because no debug messages come to my LogCat (I'm sure, log filter is right, and I tried adding Toast show), as well as no errors come out.
I will be really glad of any help, because I spend few hours and tried few lessons...
I have done everything by a lesson, but changed the names and classes. There are my classes:
Provider:
public class WidgetProvider extends AppWidgetProvider {
private static final String LOGIN_CLICKED = "com.example.intent.action.LOGIN_CLICKED";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.d("my_log", "onUpdate");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
remoteViews.setOnClickPendingIntent(R.id.button_login, buildButtonPendingIntent(context, LOGIN_CLICKED));
pushWidgetUpdate(context, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context, String action) {
Log.d("my_log", "buildButtonPendingIntent");
Intent intent = new Intent();
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
}
Receiver:
public class WidgetReceiver extends BroadcastReceiver {
private static final String LOGIN_CLICKED = "com.example.intent.action.LOGIN_CLICKED";
@Override
public void onReceive(Context context, Intent intent) {
Log.d("my_log", "onRecieve");
if(intent.getAction().equalsIgnoreCase(LOGIN_CLICKED)){
makeLogin(context);
}
}
private void makeLogin(Context context) {
Log.d("my_log", "MakeLogin");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
//remoteViews.setImageViewResource(R.id.widget_image, getImageToSet());
//REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
remoteViews.setOnClickPendingIntent(R.id.button_login, WidgetProvider.buildButtonPendingIntent(context, LOGIN_CLICKED));
WidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
}
}
My manifest:
<application
android:label="TwiWi Twitter Widget">
<receiver
android:name="WidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
<receiver
android:name="WidgetReceiver"
android:label="widgetBroadcastReceiver">
<intent-filter>
<action android:name="com.example.intent.action.LOGIN_CLICKED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
</application>
And my xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://ift.tt/nIICcg"
android:minWidth="294dip"
android:minHeight="146dip"
android:updatePeriodMillis="1000000"
android:initialLayout="@layout/activity_main" >
</appwidget-provider>
If you think I messed with layout or button name, there is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="@android:color/white"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView android:id="@+id/widget_textview"
android:text="Hello Widget"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal|center"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/button_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="Button" />
</LinearLayout>
Tiada ulasan:
Catat Ulasan