Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 594|回复: 0

[实例教程]Android widget源码

[复制链接]

该用户从未签到

发表于 2011-10-22 12:25:19 | 显示全部楼层 |阅读模式
今天我们就来说说widget程序里的源码,在这里大家可能看过了很多是源码,但是还是希望看看我的这个源码,如果有写的不好的地方还望指教。在这里就不多说废话了,还是把代码贴出来。

我们来看看ConfiguresActivity package dev.ty.shortcall;



import android.app.Activity;

import android.app.TabActivity;

import android.appwidget.AppWidgetManager;

import android.content.ContentResolver;

import android.content.Intent;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.database.Cursor;

import android.os.Bundle;

import android.provider.Contacts;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.SimpleCursorAdapter;

import android.widget.TabHost;

import android.widget.TextView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.TabHost.TabSpec;



/**

* 作用:配置快速拨号联系人信息

*

* @author TANG YONG

* @version 1.0 2010-06-21

*

*/

public class ConfiguresActivity extends TabActivity implements OnItemClickListener, OnClickListener {

private static final String TAG = "ConfiguresActivity";

public final static String PRE_NAME = "Configure";



private static final String[] CONTACT_PROJ = new String[]{

Contacts.People._ID,

Contacts.People.NAME,

Contacts.People.NUMBER

};



private Cursor cursor;



private TabHost tabHost;

private ListView contactList;

private EditText nameEdit;

private EditText numberEdit;

private Button saveBtn;



private int widgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

//private String name, number;





@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);



tabHost = getTabHost();

LayoutInflater.from(this).inflate(R.layout.configures, tabHost.getTabContentView(), true);



TabSpec tsContact = tabHost.newTabSpec("contact");

tsContact.setIndicator(getString(R.string.contactTabText), getResources().getDrawable(R.drawable.globe));

tsContact.setContent(R.id.contactList);

tabHost.addTab(tsContact);



TabSpec tsInput = tabHost.newTabSpec("input");

tsInput.setIndicator(getString(R.string.inputTabText), getResources().getDrawable(R.drawable.tag));

tsInput.setContent(R.id.inputLinear);

tabHost.addTab(tsInput);



tabHost.setCurrentTab(0);

contactList = (ListView)findViewById(R.id.contactList);



// 处理获取联系人列表

dealContactList(contactList);



setContentView(tabHost);



// 获取控件信息;

nameEdit = (EditText)findViewById(R.id.cfgContactEdit);

numberEdit = (EditText)findViewById(R.id.cfgPhoneEdit);



saveBtn = (Button)findViewById(R.id.saveBtn);

contactList.setOnItemClickListener(this);

saveBtn.setOnClickListener(this);



// 获取widgetId信息

widgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

setConfigureResult(Activity.RESULT_CANCELED);

if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {

finish();

return;

}



if(savedInstanceState != null){

nameEdit.setText(savedInstanceState.getString("name"));

numberEdit.setText(savedInstanceState.getString("number"));

}

}



private void dealContactList(ListView lv) {

// TODO Auto-generated method stub

Log.d(TAG, "deal with contact list");

ContentResolver cr = getContentResolver();

cursor = cr.query(Contacts.People.CONTENT_URI, CONTACT_PROJ, Contacts.People.NUMBER + " is not null", null, Contacts.People.DEFAULT_SORT_ORDER);

cursor.moveToFirst();



SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contactitems, cursor, new String[]{Contacts.People.NAME, Contacts.People.NUMBER}, new int[]{R.id.itemNameText, R.id.itemNumberText});



lv.setAdapter(adapter);

}



public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {

// TODO Auto-generated method stub

TextView nameText = (TextView)v.findViewById(R.id.itemNameText);

TextView numberText = (TextView)v.findViewById(R.id.itemNumberText);



String name = nameText.getText().toString();

String number = numberText.getText().toString();



if (number != null && number.length() > 0){

saveResult(name, number);

}

}



/**

*

*/

public void onClick(View v) {

// TODO Auto-generated method stub

if (v.getId() == R.id.saveBtn){



String name = nameEdit.getText().toString();

String number = numberEdit.getText().toString();



if (number != null && number.length() > 0){

saveResult(name, number);

}

}

}



/**

* 保存数据

* @param contactName

* @param contactNumber

*/

private void saveResult(String contactName, String contactNumber) {

// TODO Auto-generated method stub

AppWidgetManager manager = AppWidgetManager.getInstance(this);



// 存储信息至本地

SharedPreferences sp = getSharedPreferences(PRE_NAME + widgetId, 0);

Editor editor = sp.edit();

editor.putString("title", contactName);

editor.putString("phoneNumber", contactNumber);

editor.commit();



setConfigureResult(Activity.RESULT_OK);

finish();



ShortCallWidget.updateAppWidget(this, manager, widgetId);

}



/**

* Convenience method to always include {@link #widgetId} when setting

* the result {@link Intent}.

*/

public void setConfigureResult(int resultCode) {

final Intent data = new Intent();

data.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

setResult(resultCode, data);

}

}
我们现在来看看ShortCallWidget: package dev.ty.shortcall;



import android.app.PendingIntent;

import android.appwidget.AppWidgetManager;

import android.appwidget.AppWidgetProvider;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

import android.net.Uri;

import android.util.Log;

import android.widget.RemoteViews;



/**

* 快速拔号小程序

* @author TANGYONG

* @version 1.0 2010-07-6-08

*/

public class ShortCallWidget extends AppWidgetProvider {

private static final String TAG = "ShortCallWidget";



@Override

public void onUpdate(Context context, AppWidgetManager appWidgetManager,

int[] appWidgetIds) {

// TODO Auto-generated method stub

super.onUpdate(context, appWidgetManager, appWidgetIds);

Log.d(TAG, "login the widget");



int count = appWidgetIds.length;



for(int i = 0; i < count; i++){

int widgetId = appWidgetIds;



updateAppWidget(context, appWidgetManager, widgetId);

}

}



public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int widgetId){

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);



SharedPreferences sp = context.getSharedPreferences(ConfiguresActivity.PRE_NAME + widgetId, 0);

String telUri = "tel:" + sp.getString("phoneNumber", "");

String title = sp.getString("title", "");



views.setTextViewText(R.id.widNameText, title);

Uri uri = Uri.parse(telUri);



// Connect click intent to launch details dialog

Intent intent = new Intent(Intent.ACTION_DIAL, uri);

PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

views.setOnClickPendingIntent(R.id.widImageCall, pending);



//更新控件

appWidgetManager.updateAppWidget(widgetId, views);



}

}
我们最后来看看androidmanifest.xml <?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="dev.ty.shortcall" android:versionCode="1" android:versionName="1.0">

<application android:label="@string/app_name" android:icon="@drawable/shortcall">



<receiver android:icon="@drawable/shortcall" android:name="ShortCallWidget"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>

</intent-filter>

<meta-data android:resource="@xml/shortcallwidget"

android:name="android.appwidget.provider"></meta-data>

</receiver>

<activity android:name="ConfiguresActivity" android:label="@string/shortCallCfg"

android:icon="@drawable/shortcall" android:theme="@android:style/Theme.NoTitleBar">

<intent-filter>

<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"></action>

</intent-filter>

</activity>

</application>

<uses-sdk android:minSdkVersion="3" />



<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>



<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

</manifest>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-12-23 09:42 , Processed in 1.113311 second(s), 34 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表