程序运行截图:
一、Widget的创建步骤
widget:桌面小控件
1 写一个类extends AppWidgetProvider
2 在清单文件件中注册:
<receiver android:name=".ExampleAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver>
3 在res/xml创建example_appwidget_info.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:initialLayout="@layout/example_appwidget"
>
</appwidget-provider>
4 指定布局 example_appwidget.xml
以上所涉及到的内容都可以在android的官网中找到,其实这里我只是作揖了以下简单翻译而已............
二、生命周期
生命周期:
1 添加到桌面:
onEnabled() --> onUpdate()
2 删除
onDeleted()----> onDisabled()
如果桌面已经有一个了widget的实例存在,再次添加onUpdate()
删除之后,如果桌面上还有widget的实例存在,只会调用onDeleted().
三、以更新时间为例,讲解widget的使用
按照一中所说的方法创建好一个widget
然后
1)ExampleAppWidgetProvider
package com.njupt.widget3;
import java.text.SimpleDateFormat;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class ExampleAppWidgetProvider extends AppWidgetProvider {
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Intent intent = new Intent(context,MyService.class);
context.startService(intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
long date = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String text = format.format(date);
views.setTextViewText(R.id.tv_time,text);
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, 0);
views.setOnClickPendingIntent(R.id.tv_time, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Intent intent = new Intent(context,MyService.class);
context.stopService(intent);
}
}
2)MyService
package com.njupt.widget3;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;
public class MyService extends Service {
private Timer timer;
private TimerTask task = new TimerTask() {
@Override
public void run() {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
RemoteViews views = new RemoteViews(getPackageName(), R.layout.example_appwidget);
long date = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String text = format.format(date);
views.setTextViewText(R.id.tv_time,text);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 100, intent, 0);
views.setOnClickPendingIntent(R.id.tv_time, pendingIntent);
ComponentName provider = new ComponentName(getApplicationContext(), ExampleAppWidgetProvider.class);
appWidgetManager.updateAppWidget(provider, views);
}
};
public void onCreate() {
timer = new Timer();
timer.schedule(task, 1000,1000);
};
@Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
task = null;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
3)最后不要忘了去AndroidManifest.xml中把service给注册上。。。

383

被折叠的 条评论
为什么被折叠?



