android基础---->WidGet的使用
Widget是一个可以添加在别的应用程序中的”小部件”,我们可以使用自定义的Widget远程控制我们的程序做一些事情。一般用于在桌面上添加一个小部件,现在我们开始小部件的学习。
目录导航:
WidGet的简要说明
WidGet的实例代码
友情链接
WidGet的简要说明
一、 WidGet的特点:
轻量:它们一般都很小,在终端上嵌入非常方便,运行快速。
形式多:Widget可以以多种形式呈现出来,幻灯秀、视频、地图、新闻、小游戏等等
功能巨:别看它们小,却服务周到,它可以为你报告新闻,天气预报网站,Widget会将信息主动带给你
个性化:Widget更像一个属于我们每个人的魔方,任由用户聚合
二、 WidGet的使用步骤:
绘制小组件的布局:在layout中创建一个布局文件
配置widget的基本属性:创建一个资源文件夹,添加一个widget属性的配置xml文件
定义AppWidgetProvider:创建一个继承AppWidgetProvider的java类
提供Configuration Activity:可以在MainActiviy中使用
WidGet的实例代码
按照上述所说,我们开始WidGet的实例说明:
一、 在layout中创建一个WidGet的布局文件:widget.xml
android:layout_width="match_parent" android:layout_height="match_parent" android:background="#09C" android:padding="@dimen/widget_margin"> android:id="@+id/appwidget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_margin="8dp" android:background="#09C" android:contentDescription="@string/appwidget_text" android:text="@string/appwidget_text" android:textColor="#ffffff" android:textSize="24sp" android:textStyle="bold|italic" />
二、 创建一个资源文件夹xml,添加一个widget属性的配置xml文件:widget_info.xml
android:configure="com.example.linux.listviewtest.MainActivity" android:initialKeyguardLayout="@layout/widget" android:initialLayout="@layout/widget" android:minHeight="40dp" android:minWidth="250dp" android:previewImage="@drawable/example_appwidget_preview" android:resizeMode="horizontal|vertical" android:updatePeriodMillis="86400000" android:widgetCategory="home_screen">
三、 创建一个继承AppWidgetProvider的java类:widget
package xml;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.example.linux.listviewtest.TimerService;
/**
* writer: huhx
* Implementation of App Widget functionality.
*/
public class widget extends AppWidgetProvider {
private final static String TAG = "WidGetTest";
public widget() {
Log.i(TAG, "widget constructor.");
}
// 当一个或多个小组件的实例被删除时调用
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Log.i(TAG, "on deleted");
super.onDeleted(context, appWidgetIds);
}
// 当最后一个这样的小组件被删除
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.i(TAG, "on disable");
context.stopService(new Intent(context, TimerService.class));
}
// 当这样的小组件被初始化时,也就是当小组件被拖到桌面
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.i(TAG, "on enable");
context.startService(new Intent(context, TimerService.class));
}
// to dispatch calls to the various other methods on AppWidgetProvider.
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "on receive");
super.onReceive(context, intent);
}
// 小组件的更新
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
四、 创建一个更新时间的服务类:TimerService
package com.example.linux.listviewtest;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import xml.widget;
public class TimerService extends Service {
private final static String TAG = "WidGetTest";
private Timer timer;
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public TimerService() {
Log.i(TAG, "timer service constructor");
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "on bind");
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "on create");
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
updateViews();
}
}, 0, 1000);
}
private void updateViews() {
Log.i(TAG, "update views");
String time = simpleDateFormat.format(new Date());
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
remoteViews.setTextViewText(R.id.appwidget_text, time);
AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName name = new ComponentName(getApplicationContext(), widget.class);
manager.updateAppWidget(name, remoteViews);
}
@Override
public void onDestroy() {
super.onDestroy();
timer = null;
}
}
五、 在AndroidManifest.xml中,注册服务和接收器:
android:name=".TimerService" android:enabled="true" android:exported="true" /> android:name="android.appwidget.provider" android:resource="@xml/widget_info" /> 六、 在MainActivity中使用: package com.example.linux.listviewtest; import android.appwidget.AppWidgetManager; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { private final static String TAG = "WidGetTest"; int mAppWidgetId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG, " on main create ... "); setResult(RESULT_CANCELED); // Find the widget id from the intent. Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) { mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); } // If they gave us an intent without the widget id, just bail. if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { finish(); } // return OK Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); setResult(RESULT_OK, resultValue); finish(); } } 七、 运行效果如下截图以及打印的日志如下: 第一次拖到桌面,打印如下: 03-26 09:21:03.138 27090-27090/com.example.linux.listviewtest I/WidGetTest: widget constructor. 03-26 09:21:03.140 27090-27090/com.example.linux.listviewtest I/WidGetTest: on receive 03-26 09:21:03.140 27090-27090/com.example.linux.listviewtest I/WidGetTest: on enable 03-26 09:21:03.156 27090-27090/com.example.linux.listviewtest I/WidGetTest: timer service constructor 03-26 09:21:03.156 27090-27090/com.example.linux.listviewtest I/WidGetTest: on create 03-26 09:21:03.161 27090-27090/com.example.linux.listviewtest I/WidGetTest: widget constructor. 03-26 09:21:03.161 27090-27090/com.example.linux.listviewtest I/WidGetTest: on receive 03-26 09:21:03.173 27090-27483/com.example.linux.listviewtest I/WidGetTest: update views 03-26 09:21:03.198 27090-27090/com.example.linux.listviewtest I/WidGetTest: on main create ... 03-26 09:21:03.237 27090-27090/com.example.linux.listviewtest I/WidGetTest: widget constructor. 03-26 09:21:03.237 27090-27090/com.example.linux.listviewtest I/WidGetTest: on receive 03-26 09:21:04.173 27090-27483/com.example.linux.listviewtest I/WidGetTest: update views 03-26 09:21:05.214 27090-27483/com.example.linux.listviewtest I/WidGetTest: update views....... 当把它移除,此时桌面没有小组件,打印日志如下: 03-26 09:24:10.719 30093-30093/com.example.linux.listviewtest I/WidGetTest: widget constructor. 03-26 09:24:10.719 30093-30093/com.example.linux.listviewtest I/WidGetTest: on receive 03-26 09:24:10.719 30093-30093/com.example.linux.listviewtest I/WidGetTest: on deleted 03-26 09:24:10.765 30093-30093/com.example.linux.listviewtest I/WidGetTest: widget constructor. 03-26 09:24:10.765 30093-30093/com.example.linux.listviewtest I/WidGetTest: on receive 03-26 09:24:10.766 30093-30093/com.example.linux.listviewtest I/WidGetTest: on disable 友情链接 WidGet测试代码下载 访问密码 f1c1