添加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典型代码系列(二十八)------通话记录的操作
  2. Android Bitmap 缩放 旋转 水印 裁剪操作
  3. Android 通知栏系列....
  4. Android数据库操作查询中Cursor类的问题
  5. android 操作sqllite 数据库
  6. android 的Handler处理UI主线程外的耗时操作
  7. Android 消息通知栏Notification使用和权限
  8. Android 8.1 通知的变化
  9. android用异步操作AsyncTask编写文件查看器

随机推荐

  1. android 连接外部服务
  2. Build android source code 3 -- 编译
  3. [置顶] Android学习网站和博客汇总
  4. Android耳机按键监听
  5. Android(安卓)AsyncTask原理分析
  6. android 获取时间
  7. webservice获取手机归属地
  8. Android(安卓)Asynchronous Http Client-
  9. android 拦截鼠标按键
  10. android 全屏显示