Widget分区域点击

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minHeight="72dp"
    android:minWidth="72dp"
    android:initialLayout="@layout/widget"
    <span style="background-color: #ff0000;">android:configure="com.example.changewidget.ChangeWidgetActivity"</span>

    android:updatePeriodMillis="84600000" >
</appwidget-provider>
 

Widget配置文件:

package com.example.changewidget;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RemoteViews;

public class ChangeWidgetActivity extends Activity {

    private static final String TAG = "tracy";
	private static final String ATV = "ChangeWidgetActivity		";
	int mAppWidgetId;  
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.edit);
        
        /**
         * 表明创建widget失败
         */
        setResult(RESULT_CANCELED);
        
        /**
         * 取出该配置文件对应的widgetId
         */
        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();  
        }  
        
        /**
         * 设置配置页面的点击效果
         */
        Log.i(TAG,ATV+"onCreate mId="+mAppWidgetId);  
		findViewById(R.id.imageView1).setOnClickListener(clicklisten);
        findViewById(R.id.imageView2).setOnClickListener(clicklisten);
        findViewById(R.id.imageView3).setOnClickListener(clicklisten);
        
    }
    OnClickListener clicklisten = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			int id = 0;
			switch (v.getId()) {
			case R.id.imageView1:
				id = R.drawable.bssd1;
				break;
			case R.id.imageView2:
				id = R.drawable.bssd2;
				break;
			default:
				id = R.drawable.bsyl1;
				break;
			}
			
			/**
			 * 设置widget的布局文件
			 */
			RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.widget);
			
	    	remoteViews.setImageViewResource(R.id.icon, id);
	    	/**
	    	 * 设置widget的分区域点击
	    	 */
	    	Intent intent = new Intent(ChangeWidgetActivity.this,PageOne.class);
			intent.setAction(TAG+mAppWidgetId);
			intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppWidgetId);
			PendingIntent pendingIntent = PendingIntent.getActivity(ChangeWidgetActivity.this, 0,
					intent, 0);
	    	remoteViews.setOnClickPendingIntent(R.id.icon, pendingIntent);
	    	
			Intent intent2 = new Intent(ChangeWidgetActivity.this, PageTwo.class);
			intent2.setAction(TAG+mAppWidgetId);
			intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppWidgetId);
	    	PendingIntent pendingIntent2 = PendingIntent.getActivity(ChangeWidgetActivity.this, 0,
					intent2, 0);
	    	remoteViews.setOnClickPendingIntent(R.id.icon2, pendingIntent2);
	    	
	    	/**
	    	 * 更新对应widgetId的widget
	    	 */
	    	AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ChangeWidgetActivity.this);
	    	appWidgetManager.updateAppWidget(mAppWidgetId, remoteViews);
	    	Intent resultValue = new Intent();  
	        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,  
	                mAppWidgetId);  
	        setResult(RESULT_OK, resultValue);  
	        finish();  
		}
	};

}
 

在xml标志配置文件:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minHeight="72dp"
    android:minWidth="72dp"
    android:initialLayout="@layout/widget"
    <span style="background-color: #ff0000;">android:configure="com.example.changewidget.ChangeWidgetActivity"</span>

    android:updatePeriodMillis="84600000" >
</appwidget-provider>
 

相关推荐