Android Widget 设计与开发(3)

view plaincopy to clipboardprint?
package org.anymobile.demo.service;  
import java.util.ArrayList;  
import Android.app.Service;  
import android.appwidget.AppWidgetManager;  
import android.content.BroadcastReceiver;  
import android.content.ComponentName;  
import android.content.Context;  
import android.content.Intent;  
import android.content.IntentFilter;  
import android.os.IBinder;  
import android.util.Log;  
import android.view.View;  
import android.widget.RemoteViews;  
import org.anymobile.demo.Globals;  
import org.anymobile.demo.R;  
import org.anymobile.demo.widget.UpdateAppWidgetProvider;  
public class UpdateService extends Service  
{  
    public static final String TAG = "ANYMOBILE-DEMO--UpdateService";  
      
    private ArrayList<String> mList;  
    private int mCount;  
    private int mId;  
      
    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver()  
    {  
        @Override 
        public void onReceive(Context context, Intent intent)  
        {  
            String action = intent.getAction();  
            Log.d(TAG, "onReceive() " + action);  
              
            if (action.equals(Globals.ACTION_APP_WIDGET_RELOAD))  
            {  
                doReload();  
            }  
        }  
    };  
    @Override 
    public void onCreate()  
    {  
        Log.d(TAG, "onCreate()");  
        super.onCreate();  
          
        reloadQueue();  
          
        IntentFilter filter = new IntentFilter();  
        filter.addAction(Globals.ACTION_APP_WIDGET_RELOAD);  
        registerReceiver(mIntentReceiver, filter);  
    }  
      
    @Override 
    public void onStart(Intent intent, int startId)  
    {  
        super.onStart(intent, startId);  
        String action = intent.getAction();  
        Log.d(TAG, "onStart() " + action);  
        if (action.equals(Globals.ACTION_APP_WIDGET_PREV))  
        {  
            doPrev();  
        }  
        else if (action.equals(Globals.ACTION_APP_WIDGET_NEXT))  
        {  
            doNext();  
        }  
        else// if (action.equals(Globals.ACTION_APP_WIDGET_SERVICE))  
        {  
            notifyWidget();  
        }  
    }  
      
    private void notifyWidget()  
    {  
        Log.d(TAG, "notifyWidget()");  
          
        ComponentName widget = new ComponentName(this, UpdateAppWidgetProvider.class);  
        RemoteViews updateViews = buildUpdate(this);  
          
        AppWidgetManager manager = AppWidgetManager.getInstance(this);  
        manager.updateAppWidget(widget, updateViews);  
    }  
    @Override 
    public void onDestroy()  
    {  
        Log.d(TAG, "onDestroy()");  
          
        unregisterReceiver(mIntentReceiver);  
          
        super.onDestroy();  
    }  
    @Override 
    public IBinder onBind(Intent intent)  
    {  
        Log.d(TAG, "onBind()");  
        return null;  
    }  
      
    private RemoteViews buildUpdate(Context context)  
    {  
        RemoteViews updateViews =   
            new RemoteViews(context.getPackageName(), R.layout.update_appwidget);  
        String item = null;  
          
        if (mCount > 0)  
        {  
            item = mList.get(mId);  
            if (item != null)  
            {  
                updateViews.setViewVisibility(R.id.app_widget_content, View.GONE);  
                updateViews.setViewVisibility(R.id.app_widget_message, View.VISIBLE);  
                  
//              updateViews.setViewVisibility(R.id.app_widget_content, View.VISIBLE);  
//              updateViews.setViewVisibility(R.id.app_widget_message, View.GONE);  
//                
//              updateViews.setImageViewResource(R.id.update_appwidget_icon, item.getTypeIconId());  
//              updateViews.setTextViewText(R.id.update_appwidget_name, item.getNickName());  
//              updateViews.setTextViewText(R.id.update_appwidget_time, item.getModifyTime());  
//              updateViews.setTextViewText(R.id.update_appwidget_content, item.getMessage());  
                  
                updateViews.setTextViewText(R.id.widget_message, item);  
            }  
        }  
        if (item == null)  
        {  
            updateViews.setViewVisibility(R.id.app_widget_content, View.GONE);  
            updateViews.setViewVisibility(R.id.app_widget_message, View.VISIBLE);  
              
            updateViews.setTextViewText(R.id.widget_message,   
                context.getText(R.string.app_widget_error_message));  
        }  
        Log.d(TAG, "buildUpdate: layoutId = " + updateViews.getLayoutId() +   
            "; count = " + mCount + "; id = " + mId);  
          
        return updateViews;  
    }  
      
    private void doReload()  
    {  
        Log.d(TAG, "doReload()");  
        reloadQueue();  
          
        notifyWidget();  
    }  
      
    private void reloadQueue()  
    {  
        mList = new ArrayList<String>();  
        String[] arr = {"aa", "bb", "cc", "dd"};  
        for (int i = 0; i < arr.length; i++)  
        {  
            mList.add(arr[i]);  
        }  
          
        if (mList != null)  
        {  
            mCount = mList.size();  
        }  
        else 
        {  
            mCount = 0;  
        }  
        mId = 0;  
          
        //TODO check login and poll updates from buddie list  
    }  
      
    private void doPrev()  
    {  
        Log.d(TAG, "doPrev()");  
        mId -= 1;  
        if (mId < 0)  
        {  
            mId = mCount - 1;  
        }  
        notifyWidget();  
    }  
      
    private void doNext()  
    {  
        Log.d(TAG, "doNext()");  
        mId += 1;  
        if (mId > mCount - 1)  
        {  
            mId = 0;  
        }  
        notifyWidget();  
    }  

package org.anymobile.demo.service;
import java.util.ArrayList;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
import org.anymobile.demo.Globals;
import org.anymobile.demo.R;
import org.anymobile.demo.widget.UpdateAppWidgetProvider;
public class UpdateService extends Service
{
 public static final String TAG = "ANYMOBILE-DEMO--UpdateService";
 
 private ArrayList<String> mList;
 private int mCount;
 private int mId;
 
 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver()
 {
        @Override
        public void onReceive(Context context, Intent intent)
        {
         String action = intent.getAction();
         Log.d(TAG, "onReceive() " + action);
         
         if (action.equals(Globals.ACTION_APP_WIDGET_RELOAD))
         {
          doReload();
         }
        }
 };
 @Override
 public void onCreate()
 {
     Log.d(TAG, "onCreate()");
  super.onCreate();
  
  reloadQueue();
  
  IntentFilter filter = new IntentFilter();
  filter.addAction(Globals.ACTION_APP_WIDGET_RELOAD);
  registerReceiver(mIntentReceiver, filter);
 }
 
 @Override
 public void onStart(Intent intent, int startId)
 {
  super.onStart(intent, startId);
     String action = intent.getAction();
     Log.d(TAG, "onStart() " + action);
     if (action.equals(Globals.ACTION_APP_WIDGET_PREV))
     {
      doPrev();
     }
     else if (action.equals(Globals.ACTION_APP_WIDGET_NEXT))
     {
      doNext();
     }
     else// if (action.equals(Globals.ACTION_APP_WIDGET_SERVICE))
     {
      notifyWidget();
     }
 }
 
 private void notifyWidget()
 {
     Log.d(TAG, "notifyWidget()");
     
  ComponentName widget = new ComponentName(this, UpdateAppWidgetProvider.class);
  RemoteViews updateViews = buildUpdate(this);
  
  AppWidgetManager manager = AppWidgetManager.getInstance(this);
  manager.updateAppWidget(widget, updateViews);
 }
 @Override
 public void onDestroy()
 {
     Log.d(TAG, "onDestroy()");
     
        unregisterReceiver(mIntentReceiver);
       
  super.onDestroy();
 }
 @Override
 public IBinder onBind(Intent intent)
 {
     Log.d(TAG, "onBind()");
  return null;
 }
 
 private RemoteViews buildUpdate(Context context)
 {
  RemoteViews updateViews =
   new RemoteViews(context.getPackageName(), R.layout.update_appwidget);
  String item = null;
  
  if (mCount > 0)
  {
   item = mList.get(mId);
   if (item != null)
   {
    updateViews.setViewVisibility(R.id.app_widget_content, View.GONE);
    updateViews.setViewVisibility(R.id.app_widget_message, View.VISIBLE);
    
//    updateViews.setViewVisibility(R.id.app_widget_content, View.VISIBLE);
//    updateViews.setViewVisibility(R.id.app_widget_message, View.GONE);
//    
//    updateViews.setImageViewResource(R.id.update_appwidget_icon, item.getTypeIconId());
//    updateViews.setTextViewText(R.id.update_appwidget_name, item.getNickName());
//    updateViews.setTextViewText(R.id.update_appwidget_time, item.getModifyTime());
//    updateViews.setTextViewText(R.id.update_appwidget_content, item.getMessage());
    
    updateViews.setTextViewText(R.id.widget_message, item);
   }
  }
  if (item == null)
  {
   updateViews.setViewVisibility(R.id.app_widget_content, View.GONE);
   updateViews.setViewVisibility(R.id.app_widget_message, View.VISIBLE);
   
   updateViews.setTextViewText(R.id.widget_message,
    context.getText(R.string.app_widget_error_message));
  }
     Log.d(TAG, "buildUpdate: layoutId = " + updateViews.getLayoutId() +
      "; count = " + mCount + "; id = " + mId);
  
  return updateViews;
 }
 
 private void doReload()
 {
     Log.d(TAG, "doReload()");
  reloadQueue();
  
  notifyWidget();
 }
 
 private void reloadQueue()
 {
  mList = new ArrayList<String>();
  String[] arr = {"aa", "bb", "cc", "dd"};
  for (int i = 0; i < arr.length; i++)
  {
   mList.add(arr[i]);
  }
  
  if (mList != null)
  {
   mCount = mList.size();
  }
  else
  {
   mCount = 0;
  }
  mId = 0;
  
  //TODO check login and poll updates from buddie list
 }
 
 private void doPrev()
 {
     Log.d(TAG, "doPrev()");
  mId -= 1;
  if (mId < 0)
  {
   mId = mCount - 1;
  }
  notifyWidget();
 }
 
 private void doNext()
 {
     Log.d(TAG, "doNext()");
  mId += 1;
  if (mId > mCount - 1)
  {
   mId = 0;
  }
  notifyWidget();
 }
}
 

#UpdateAppWidgetProvider.java

view plaincopy to clipboardprint?
package org.anymobile.demo.widget;  
   
import android.app.PendingIntent;  
import android.appwidget.AppWidgetManager;  
import android.appwidget.AppWidgetProvider;  
import android.content.ComponentName;  
import android.content.Context;  
import android.content.Intent;  
import android.util.Log;  
import android.view.View;  
import android.widget.RemoteViews;  
   
import org.anymobile.demo.Globals;  
import org.anymobile.demo.R;  
import org.anymobile.demo.service.UpdateService;  
   
public class UpdateAppWidgetProvider extends AppWidgetProvider  
{  
    public static final String TAG = "ANYMOBILE-DEMO-UpdateAppWidgetProvider";  
      
    public static final String APP_WIDGET_UPDATE = "appwidgetupdate";  
    public static final ComponentName APPWIDGET_COMPONENT_NAME =  
        new ComponentName("org.anymobile.demo",   
            "org.anymobile.demo.widget.UpdateAppWidgetProvider");  
    @Override 
    public void onReceive(Context context, Intent intent)  
    {  
        Log.d(TAG, "onReceive() " + intent.getAction());  
        super.onReceive(context, intent);  
    }  
    @Override 
    public void onEnabled(Context context)  
    {  
        Log.d(TAG, "onEnabled()");  
        super.onEnabled(context);  
    }  
    @Override 
    public void onDisabled(Context context)  
    {  
        Log.d(TAG, "onDisabled()");  
        super.onDisabled(context);  
    }  
    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)  
    {  
        Log.d(TAG, "onUpdate()");  
          
        defaultAppWidget(context, appWidgetIds);  
          
        context.startService(new Intent(Globals.ACTION_APP_WIDGET_SERVICE));  
    }  
      
    private void defaultAppWidget(Context context, int[] appWidgetIds)  
    {  
        final RemoteViews views =   
            new RemoteViews(context.getPackageName(), R.layout.update_appwidget);  
          
        views.setViewVisibility(R.id.app_widget_content, View.GONE);  
        views.setViewVisibility(R.id.app_widget_message, View.VISIBLE);  
          
        // Link actions buttons to intents  
        linkButtons(context, views);  
          
        pushUpdate(context, appWidgetIds, views);  
    }  
      
    private void linkButtons(Context context, RemoteViews views)  
    {  
        Intent intent;  
        PendingIntent pendingIntent;  
        final ComponentName serviceName = new ComponentName(context, UpdateService.class);  
          
        intent = new Intent(Globals.ACTION_APP_WIDGET_PREV);  
        intent.setComponent(serviceName);  
        pendingIntent = PendingIntent.getService(context,  
                0 /* no requestCode */, intent, 0 /* no flags */);  
        views.setOnClickPendingIntent(R.id.widget_btn_prev_page, pendingIntent);  
        intent = new Intent(Globals.ACTION_APP_WIDGET_NEXT);  
        intent.setComponent(serviceName);  
        pendingIntent = PendingIntent.getService(context,  
                0 /* no requestCode */, intent, 0 /* no flags */);  
        views.setOnClickPendingIntent(R.id.widget_btn_next_page, pendingIntent);  
    }  
      
    private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views)  
    {  
        final AppWidgetManager gm = AppWidgetManager.getInstance(context);  
        if (appWidgetIds != null)  
        {  
            gm.updateAppWidget(appWidgetIds, views);  
        }  
        else 
        {  
            gm.updateAppWidget(APPWIDGET_COMPONENT_NAME, views);  
        }  
    }  
      
    void notifyChange(UpdateService service, String what)  
    {  
        //  
    }  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsgww.html