1.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.Demo2" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="com.example.widgets.TimeWidgetProvider"> <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.widgets.TimeService"></service> </application> <uses-permission android:name="android.permission.CALL_PHONE"/> </manifest>
</pre></p><p><pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:initialLayout="@layout/time_appwidget" />
<?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" android:background="@drawable/rectangle" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="widget" android:id="@+id/textView" /> </LinearLayout>
package com.example.widgets; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; public class TimeWidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { } @Override public void onDeleted(Context context, int[] appWidgetIds) {//当删除一个widget时,执行该方法 } @Override public void onEnabled(Context context) { context.startService(new Intent(context,TimeService.class)); } @Override public void onDisabled(Context context) {//当同类型的widget都删除后,执行该方法 context.stopService(new Intent(context,TimeService.class)); } }
5.书写广播服务可以动态改变时间TimeService.java
package com.example.widgets; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import com.example.Demo2.R; import android.app.PendingIntent; import android.app.Service; import android.appwidget.AppWidgetManager; import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.IBinder; import android.text.format.Time; import android.widget.RemoteViews; public class TimeService extends Service{ private Timer timer; @Override public void onCreate() { super.onCreate(); timer = new Timer(); timer.schedule(new TimeUpdateMask(), 0, 1000); } final class TimeUpdateMask extends TimerTask{ @Override public void run() { SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); String time = sdf.format(new Date()); RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.time_appwidget); remoteView.setTextViewText(R.id.textView, time); PendingIntent pendingIntent = PendingIntent.getActivity( getApplicationContext(), 100, new Intent(Intent.ACTION_CALL,Uri.parse("tel:111111111")), 0); //为remoteview绑定服务 remoteView.setOnClickPendingIntent(R.id.textView, pendingIntent ); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext()); appWidgetManager.updateAppWidget( new ComponentName(getApplicationContext(), TimeWidgetProvider.class), remoteView); } } @Override public void onDestroy() { timer.cancel(); timer = null; } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }