Android Widget, not clickable
I am new to Android Dev, and this is my first widget.
What is supposed to happen is when the user clicks the widget, it turns off or on the Master Sync option.
However the widget does not do anything when clicked, and appears to not be clickable.
This is the body of the .java code, if it helps to post any of the other code please let me know.
public class MasterSync extends AppWidgetProvider {
/** Called when the activity is first created. */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
开发者_StackOverflow社区 // now label the property of the button
boolean sync = ContentResolver.getMasterSyncAutomatically();
if (sync){
ContentResolver.setMasterSyncAutomatically(false);}
else
if (!sync){
ContentResolver.setMasterSyncAutomatically(true);
}
}}
processing a click on a widget takes a bit more than just one line of code - have a look at this tutorial - it explains the basics of widgets and also how to process clicks:
Hello Widget Tutorial
onUpdate is called when the widget is run for the first time or after every specified time interval as mentioned in the xml. In order to handle the click event of a button you can use PendingIntents. You need to register your broadcast action on click of a button with the PendingIntent and then inside your onReceive()
you can write your logic to handle the broadcast which will be fired when button is clicked.
Refer this article for code snippet.
onUpdate is called when the widget is updated, not when clicked
精彩评论