AndroidWidget实践 --- EverydayTips开发(3)

这次我们实现点击widget 弹出Activity 并把当前字串传送给Activity


AndroidWidget实践 --- EverydayTips开发(3)

其实很简单就是intent的传值

但是由于是第一次做,还是遇到了很多小问题,查了不少资料总算搞定

package com.su.tipeveryday;

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.provider.ContactsContract.CommonDataKinds.Note;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

public class TipEveryDayWidget extends AppWidgetProvider {

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {// widget更新执行的方法
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		// final int N = appWidgetIds.length;//实现单个widget更新的方法,暂时不用
		// Log.d("app", "onUpdate--->Ids===" + String.valueOf(N));
		// for (int i = 0; i < N; i++) {// 如果有很多同类widget是需要遍历的(他们的id是不同的)
		// int appWidgetId = appWidgetIds[i];
		// updateAppWidget(context, appWidgetManager, appWidgetId);//
		// 更新widget的方法
		// }

		updateAppWidget(context, appWidgetManager, appWidgetIds);// 更新widget的方法

	}

	private void updateAppWidget(Context context,
			AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		// 1.1,增加跳转用activity相关 intent

		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);
		String tip = Tips.getTips();// 从Tips.java 获取警句
		views.setTextViewText(R.id.textViewWidget, tip);// 设置

		Intent intent = new Intent(context, TipEveryDayActivity.class);// 一下三句就可以启动Activity了
		intent.putExtra("TIP", tip);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				intent, PendingIntent.FLAG_CANCEL_CURRENT);

		/**
		 * 传值说明
		 * ①若该Intent带有数据,则需要将最后一个参数的值设为:FLAG_CANCEL_CURRENT eg:
		 * PendingIntent Pfullintent=PendingIntent.getActivity(this, 0,
		 * fullIntent,PendingIntent.FLAG_CANCEL_CURRENT);
		 * ②若该Intent不带数据,则最后一个参数设为0 eg: PendingIntent
		 * Pfullintent=PendingIntent.getActivity(this, 0, fullIntent, 0);
		 */
		views.setOnClickPendingIntent(R.id.textViewWidget, pendingIntent);

		Intent intent2 = new Intent("com.su.ontipclick");// 这三句就可以注册按钮事件了,确实比较麻烦
		PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0,
				intent2, 0);
		views.setOnClickPendingIntent(R.id.buttonChange, pendingIntent2);

		appWidgetManager.updateAppWidget(appWidgetIds, views);// 把widget的内容更新

	}

	public void onReceive(Context context, Intent intent) {

		if (intent.getAction().equals("com.su.ontipclick")) {
			Toast.makeText(context, "OK!!!!!", Toast.LENGTH_LONG).show();// 点击按钮会触发的事件
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_layout);// 只能通过远程对象来设置appwidget中的控件状态
			String tip = Tips.getTips();// 从Tips.java 获取警句
			remoteViews.setTextViewText(R.id.textViewWidget, tip);
			AppWidgetManager appWidgetManager = AppWidgetManager
					.getInstance(context);// 获得appwidget管理实例,用于管理appwidget以便进行更新操作
			ComponentName componentName = new ComponentName(context,
					TipEveryDayWidget.class);// 相当于获得所有本程序创建的appwidget
			appWidgetManager.updateAppWidget(componentName, remoteViews);

		}
		super.onReceive(context, intent);
	}

}

具体自己看注释吧

下面是Activity获取值 这个没什么说的

package com.su.tipeveryday;  
  
import android.app.Activity;  
import android.appwidget.AppWidgetManager;  
import android.content.Intent;  
import android.os.Bundle;  
import android.util.Log;  
import android.widget.EditText;
  
public class TipEveryDayActivity extends Activity {  
      
    int mAppWidgetId;
	private EditText editText;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        Log.i("myLog"," on WidgetConf ... ");  
        editText = (EditText)findViewById(R.id.editTextTip); 
        setResult(RESULT_CANCELED);  
          
        // Find the widget id from the intent.  
        Intent intent = getIntent();  
        
        String tip = intent.getStringExtra("TIP");//获取传来的值
		editText.setText(tip);
       
		Bundle extras = intent.getExtras();  
        if (extras != null) {  
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,  
                    AppWidgetManager.INVALID_APPWIDGET_ID);  
            Log.i("TTTTT", mAppWidgetId+"");//这个返回的屏幕上的widgetID,没增加一个会加一 我的模拟器是19,应该是有保留ID?
           
           
        }  
        Intent resultValue = new Intent();  
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,  
                mAppWidgetId);  //感觉这里的mAppWidgetId 应该是和控制知道widget有关稍后研究
          
        setResult(RESULT_OK, resultValue);  
      
    }  
}  

上代码 http://dl.dbank.com/s0bnzfb4vk



你可能感兴趣的:(android)