• Android Widget桌面组件创建


    package com.king.android.speical;
    import com.king.android.R;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.widget.RemoteViews;
    /**
     * 描述:创建widget类
     * 作者:Andy.Liu
     * 时间: 2012-7-17  上午07:01:39
     **/
    public class ExampleAppWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    for(int i=0;i<N;i++){
    int appWidgetId = appWidgetIds[i];
    String titlePrefix = WidgetActivity.loadTitlePref(context, appWidgetId);
    updateAppWidget(context, appWidgetManager, appWidgetId, titlePrefix);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
    //当桌面组件删除时调用 
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
    //删除appWidget
    final int N = appWidgetIds.length;
    for(int i =0;i<N;i++){
    WidgetActivity.deleteTitlePref(context, appWidgetIds[i]);
    }
    }
    //当AppWidtgetProvider提供的第一个组件删除时调用
    @Override
    public void onEnabled(Context context) {
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName("com.king.android.special", ".ExampleBroadCastReceiver"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }
    //当AppWidtgetProvider提供的最后一个组件删除时调用
    @Override
    public void onDisabled(Context context) {
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName("com.king.android.special", ".ExampleBroadCastReceiver"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }
    // 更新
    static void updateAppWidget(Context context,AppWidgetManager appwidgetManager,int appWidgetId,String titlePrefix){
    //构建 RemoteViews 对象对桌面组件进行更新
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);
    //更新文本内容,指定布局组件
    views.setTextViewText(R.id.appwidget_text, titlePrefix);
    //将RemoteViews更新传入到AppWidget进行更新
    appwidgetManager.updateAppWidget(appWidgetId, views);
    }
    }
    package com.king.android.speical;
    import java.util.ArrayList;
    import android.appwidget.AppWidgetManager;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    /**
     * 描述:创建广播类来接收的信息
     * 作者:Andy.Liu
     * 时间: 2012-7-17  上午07:03:30
     **/
    public class ExampleBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    //通过BroadCastReceiver来更新AppWidget
    String action = intent.getAction();
    if(action.equals(Intent.ACTION_TIMEZONE_CHANGED)||action.equals(Intent.ACTION_TIME_CHANGED)){
    AppWidgetManager gm = AppWidgetManager.getInstance(context);
    ArrayList<Integer> appWidgetIds = new ArrayList<Integer>();
    ArrayList<String> texts = new ArrayList<String>();
    WidgetActivity.loadAllTitlePrefs(context, appWidgetIds, texts);
    // 更新所有AppWidget
    final int N = appWidgetIds.size();
    for(int i=0;i<N;i++){
    ExampleAppWidgetProvider.updateAppWidget(context,gm,appWidgetIds.get(i),texts.get(i));
    }
    }
    }
    }

     package com.king.android.speical;

    import java.util.ArrayList;
    import android.app.Activity;
    import android.appwidget.AppWidgetManager;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import com.king.android.R;
    public class WidgetActivity extends Activity
    {
    private static final String PREFS_NAME = "com.king.android.ExampleAppWidgetProvider";
    private static final String PREF_PREFIX_KEY = "prefix_";
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    EditText mAppWidgetPrefix;
    public WidgetActivity()
    {
    super();
    }
    public void onCreate(Bundle icicle)
    {
    super.onCreate(icicle);
    setResult(RESULT_CANCELED);
    setContentView(R.layout.appwidget_configure);
    mAppWidgetPrefix = (EditText) findViewById(R.id.appwidget_prefix);
    findViewById(R.id.save_button).setOnClickListener(mOnClickListener);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null)
    {
    mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    }
    if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
    {
    finish();
    }
    mAppWidgetPrefix.setText(loadTitlePref(WidgetActivity.this, mAppWidgetId));
    }
    View.OnClickListener mOnClickListener = new View.OnClickListener() 
    {
    public void onClick(View v)
    {
    final Context context = WidgetActivity.this;
    String titlePrefix = mAppWidgetPrefix.getText().toString();
    saveTitlePref(context, mAppWidgetId, titlePrefix);
    //取得AppWidgetManager实例
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    //更新AppWidget
    ExampleAppWidgetProvider.updateAppWidget(context, appWidgetManager, mAppWidgetId, titlePrefix);
    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, resultValue);
    finish();
    }
    };
    static void saveTitlePref(Context context, int appWidgetId, String text)
    {
    SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
    prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
    prefs.commit();
    }
    static String loadTitlePref(Context context, int appWidgetId)
    {
    SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
    String prefix = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
    if (prefix != null)
    {
    return prefix;
    }
    else
    {
    return "xxxxxxxxxxxxxxxxx";
    }
    }
    static void deleteTitlePref(Context context, int appWidgetId)
    {
    }
    static void loadAllTitlePrefs(Context context, ArrayList<Integer> appWidgetIds, ArrayList<String> texts)
    {
    }
    }

     <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />
        <EditText
            android:id="@+id/appwidget_prefix"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />
        <Button
            android:id="@+id/save_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@android:string/ok"
        />
    </LinearLayout>

     <?xml version="1.0" encoding="utf-8"?>

    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:minWidth="100dp"
        android:minHeight="50dp"
        android:updatePeriodMillis="86400000"
        android:initialLayout="@layout/appwidget_provider"/>

     <?xml version="1.0" encoding="utf-8"?>

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/appwidget_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  • 相关阅读:
    用于 Windows8 的 Wijmo Charts 图表控件
    Silverlight 版 C1OutlookBar 初体验
    TX Text Control文字处理教程(12)MS Word中字段的导入导出操作
    TX Text Control文字处理教程(5)数据库操作
    TX Text Control文字处理教程(10)创建报价单
    TX Text Control文字处理教程(9)XML文件
    在 ActiveReports 中嵌入 Spread 控件
    TX Text Control文字处理教程(8)使用超链接
    TX Text Control文字处理教程(11)使用文本框
    2012春节快到了我们聚会吧!
  • 原文地址:https://www.cnblogs.com/liuzenglong/p/2594564.html
Copyright © 2020-2023  润新知