Pro Android Learning Note (137): Home screen Widgets (3): Configure activity

Source: Internet
Author: User
Tags home screen

The article can only be reproduced in non-commercial nature, and can not be accompanied by virtual currency, points, register and other additional conditions. Reprint must indicate the source http://blog.csdn.net/flowingflying/and the author @ kay Wind Wei.

Defined by the widget. We saw our testwidget in the widget list. When we drag the widget to the homepage. Suppose the Java class of android:configure is defined in Appwidet-provider, and the configuration activity is aroused immediately after the widget instance is created. This activity mainly completed two tasks: 1, configuration initialization data, 2, to adapt the configuration data to the widget instance.

using storage configuration data in preference

Widget data can be kept in files, Share preference, or SQLite3.

Widgets, as small tool configuration data, are often conveniently stored in preference.

Data storage and reading in preference are handled using the Birthdaystoredata class.

We are in Pro Android Learning Note (63): Preferences (7): the "Save state with preference" in the code control preferences has been described how to implement, here, review.

We need to widgetid the contents of the storage. Name. Birthdays, preference are stored as key-value pairs, and we use Name_widgetid as key. Birthdays are stored as value for information.

public class Birthdaystoredata {
private final static String Birthday_widget_provider_name = "Cn.wei.flowingflying.testwidget.provider";

//Save configuration Data : Create widget instances, save related configuration data when configured with configure Activity
public static void StoreData (Context context,int widgetid, String name,string value) {
String key = GetKey (Widgetid,name);
//The first parameter is a preferences file, which is created if it does not exist. Details for/data/data/cn.wei.flowingflying.testwidget/shared_prefs/ Cn.wei.flowingflying.testwidget.provider.xml, can be viewed in DDMS.
Editor editor = context.getsharedpreferences (Birthday_widget_provider_name, Context.mode_private). Edit ();
Editor.putstring (key, value);
Editor.commit ();

}

// Delete configuration data : Delete widget instances at the same time. Need to delete relevant data for this instance
public static void Removedata (context context, int widgetid) {
String key = Getkeybyid (context, Widgetid);
if (key = = null)
Return
Editor editor = context.getsharedpreferences (Birthday_widget_provider_name, Context.mode_private). Edit ();
Editor.remove (key);
Editor.commit ();
}
//Clear all configuration data
public static void Removealldata (context context) {
Editor editor = context.getsharedpreferences (Birthday_widget_provider_name, Context.mode_private). Edit ();
editor.clear ();
Editor.commit ();
}

//Display configuration Data : Used for our tracking in Logcat, here. We also demonstrated how to do this by polling. Show all the data, in a similar way, we can check with Widgetid the corresponding name and birthday, through a similar method, can be based on Widgetid query key. Name, birthday. Related code withheld.
public static void ShowData (context context) {
Sharedpreferences prefs = context.getsharedpreferences (Birthday_widget_provider_name, context.mode_private);
map<string, huh?

> Pairs = Prefs.getall ();
log.d ("DATA", "Total" + pairs.size () + "widgets:");
For (String Key:pairs.keySet ()) {
String value = (string) pairs.get (key);

log.d ("DATA", Key + "-" + value);
}
}

public static String Getnamebyid (context context, int widgetid) {
... ...
}

public static String Getdatebyid (context context, int widgetid) {
... ...
}

private static String GetKey (int widgetid, string name) {
Return name + "_" + Widgetid;
}

private static String Getkeybyid (Context context,int widgetid) {
... ...
}

}

Configuring initialization Data

The code for configuring configure activity is as follows:

public class Configbirthdaywidgetactivity extends activity{
private static String tag = "Configactivity";
private int mywidgetid = appwidgetmanager.invalid_appwidget_id;

@Override //Configure activity to operate as normal activity. But when aroused by appwidgetmanage, intent is the information that carries Widgetid. We get the widget ID in OnCreate ().
protected void OnCreate (Bundle savedinstancestate) {
...
Intent Intent = Getintent ();
Bundle B = Intent.getextras ();
if (b! = null) {
Mywidgetid = B.getint (appwidgetmanager.extra_appwidget_id,appwidgetmanager.invalid_appwidget_id);
}

if (Mywidgetid = = appwidgetmanager.invalid_appwidget_id) {
Toast.maketext (This, "widget Error: No valid widget ID", Toast.length_long). Show ();
Finish ();
}

}

...

//Click the method that is called after you configure the button
private void Getandstoreconfiginfo () {
... ... String name enters a name for the user, string date is the valid date entered by the user
//"1" maintains data in preference and displays all data
Birthdaystoredata.storedata (this, Mywidgetid, name, date);
Birthdaystoredata.showdata (this);
//"2" to associate configuration data with a detailed widget instance, detailed implementation see later
Birthdaystoredata.updateappwidget (this, mywidgetid,name, date);

//"3" returns the result to Appwidget Manager. To inform it that Configurator has finished.

function as Startactivityforresult () gives the return value. Notifies Appwidgetmanager that a WIDGETID has been configured to display the created widget instances on the home page
Intent resultintent = new Intent ();
Resultintent.putextra (appwidgetmanager.extra_appwidget_id, Mywidgetid);
Setresult (RESULT_OK, resultintent);

//"4" Close activity
Finish ();
}
}

Configure data to fit into widget instances

The view of the widget instance is controlled by remoteviews, and the sample is made in a static way. Code snippets such as the following:

public class Birthdaystoredata {
... ...

public static void Updateappwidget (Context context,int widgetid,string name, String date) {
//"1" To set remote View information
///1.1), Get remote View object
remoteviews views = new Remoteviews (Context.getpackagename (), r.layout.birday_widget);
///1.2), SetText () settings for remote view
Views.settextviewtext (r.id.bd_name, Widgetid + ":" + name);
Views.settextviewtext (r.id.bd_date, date);
Views.settextviewtext
(R.id.bd_days, Long.tostring (Utils.howfarindays (utils.getdate (date)));//utils is a class that handles dates
///1.3), through pendingintent set a view of the click Processing, using intent way, can open activity,service. Receiver and so on.

This sample will open a webpage  
        Intent Intent = new Intent (intent.action _view,uri.parse ("http://www.taobao.com"));
        pendingintent pi = pendingintent.getactivity (context, 0, intent, 0);
        views.setonclickpendingintent (r.id.bd_buy, pi);

       
        //" 2 "is implemented in detail on the Widgetid instance via Appwidgetmanger.


Appwidgetmanager Appwidgetmanager = appwidgetmanager.getinstance (context);
Appwidgetmanager.updateappwidget (widgetid,views);

}

public static void Updateappwidget (Context context,int widgetid) {
... ...
}
}

Sample code in: Pro Android Learning: Widget sample

RELATED Links: My Android development related articles

Pro Android Learning Note (137): Home screen Widgets (3): Configure activity

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.