添加Widget在此之前需要做一些处理操作,可以使用 配置活动

在上一篇的实现基础上,加上配置活动(configure=activity)。这时加入Widget时。会先打开一个Activity,进行配置操作,

<appwidget-provider....android:configure="com.stone.ui.AppWidgetConfigureActivity" ></appwidget-provider>


配置活动的实现:

package com.stone.ui;import android.app.Activity;import android.appwidget.AppWidgetHost;import android.appwidget.AppWidgetManager;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.stone.R;/* * 配置部件 * 在onCreate中setContentView()函数前加入setResult(RESULT_CANCLE) ,这样假设在Activity初始化完毕前按下了BACK按键,则Widget不会启动; * 在setContentView()函数之后(不一定要在onCreate中。在Activity退出前就可以),加入例如以下设置以指定须要启动的Widget */public class AppWidgetConfigureActivity extends Activity implements OnClickListener {Button config1, config2, config3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Intent intent = getIntent();Bundle extras = intent.getExtras();if (extras != null) {int widgetid = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);//从intent中得出widgetid//通知 appwdiget 的配置已取消Intent reslut = new Intent();reslut.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetid);setResult(RESULT_CANCELED, reslut);System.out.println("result cancel");}setContentView(R.layout.switch_configure);config1 = (Button) findViewById(R.id.btn_config1);config2 = (Button) findViewById(R.id.btn_config2);config3 = (Button) findViewById(R.id.btn_config3);config1.setOnClickListener(this);config2.setOnClickListener(this);config3.setOnClickListener(this);}@Overrideprotected void onResume() {super.onResume();}private void onCompletedConfigure() {Intent intent = getIntent();Bundle extras = intent.getExtras();if (extras != null) {int widgetid = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);//从intent中得出widgetid//通知 appwdiget 的配置已完毕Intent reslut = new Intent();reslut.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetid);setResult(RESULT_OK, reslut);System.out.println("result ok");finish();System.out.println("finish ok");}}@Overridepublic void onClick(View v) {if (v == config1) {//do config1} else if (v == config2) {//do config2} else if (v == config3) {//do config3}onCompletedConfigure();}}

switch_configure.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button     android:id="@+id/btn_config1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="config1"/><Button     android:id="@+id/btn_config2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="config2"/><Button     android:id="@+id/btn_config3"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="config3"/></LinearLayout>


版权声明:本文博客原创文章,博客,未经同意,不得转载。

更多相关文章

  1. android binder机制
  2. 2011.09.26(3)——— android sample之Notepad(带下划线的TestView
  3. 转Android(安卓)安全攻防(一):SEAndroid的编译
  4. Android消息机制不完全解析(下)
  5. android log机制——输出log
  6. android 5.1 屏蔽MENU键 返回键唤醒系统
  7. Android中使用查找提示
  8. Android(安卓)Juint环境配置
  9. 箭头函数的基础使用

随机推荐

  1. Android动画效果 translate、scale、alph
  2. 安卓一些错误的总结
  3. 阿里巴巴的FastJson数据解析介绍
  4. Android内核开发:如何统计系统的启动时间
  5. 继续群发Android游戏源码(再发15款)
  6. Android(安卓)Native OpenGL应用前后台切
  7. Android camera系统开发之IPC (一)
  8. Android(安卓)SD卡 路径问题
  9. [置顶] Android捕获Back键事件
  10. Android中Styles、Themes、attrs介绍和他