马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本案例主要的实现的是一个时钟小组件的编写,并且点击上面的时间的话,跳到拨打电话的界面!
1.首先画出组件,需要一个xml文件这个文件在drawable文件夹里面:rectangle.xml<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:radius="9dp"/>
<gradient android:angle="270"
android:endColor="#5EADF4"
android:startColor="#B3F0FF"
/>
<padding android:left="5dp"
android:top=" 5dp "
android:right=" 5dp "
android:bottom=" 5dp "
/>
<stroke
android:width="1dp"
android:color="#0000CC"
android:dashWidth="10dp"
android:dashGap="6dp"
/>
</shape>
2. 在res文件夹下面创建xml文件夹,并新建appwidget_info.xml:<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="200dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/activity_main"
>
</appwidget-provider>
3.布局文件的配置:activity_main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/rectangle"
>
<TextView
android:id="@+id/widget_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:text="myWidget" />
</LinearLayout>
4.写一个服务用于实现时间的显示与更新功能:TimerService.javapackage com.example.widegets_test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.widget.RemoteViews;
public class TimerService extends Service {
private Timer timer;
@Override
public void onCreate() {
// TODO Auto-generated method stub
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
//获取AppWidgetManager对象
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
//获取当前日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//把日期转化成指定字符串格式
String data = format.format(new Date());
//创建远程的view
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_main);
//修改远程view上指定控件的值
remoteViews.setTextViewText(R.id.widget_text, data);
//意图
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:36428"));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
//为远程上的指定控件设置监听
remoteViews.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
//把TimeWidget变成一个组件名称
ComponentName componentName = new ComponentName(getApplicationContext(), TimeWidget.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
}, 0,1000);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
timer.cancel();
timer = null;
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
5.写一个类继承AppWidgetProvider:TimeWidget.javapackage com.example.widegets_test;
import android.os.Bundle;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class TimeWidget extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
Log.i("TimeWidget", "onDeleted");
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
Log.i("TimeWidget", "onDisabled");
Intent service = new Intent(context,TimerService.class);
context.stopService(service);
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
Log.i("TimeWidget", "onEnabled");
Intent service = new Intent(context,TimerService.class);
context.startService(service);
super.onEnabled(context);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("TimeWidget", "onReceive");
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
6.主配置文件里应有的权限与注册:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widegets_test"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="com.example.widegets_test.TimeWidget"
>
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info"
/>
</receiver>
<service android:name="com.example.widegets_test.TimerService"></service>
</application>
</manifest>
|