android开发 - Widgets窗口小部件

来源:互联网 发布:dota2台词 知乎 编辑:程序博客网 时间:2024/06/02 04:15


Widgets指的窗口小部件  




自定义一个Widgets




做时间的窗口小部件


一个widget是一个广播接受者,需要在清单中注册








第一,有一个widget


public class MainWidget extends AppWidgetProvider  {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
super.onUpdate(context, appWidgetManager, appWidgetIds); 


/*
* 当用户从桌面上删除widgets的时候,此方法会被调用
*/
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}

/*
* 是在最后一个同类型的widgets实例被删除的时候调用 
*/
@Override
public void onDisabled(Context context) {
context.stopService(new Intent(context,UpdateDateTime.class));
}

/*
* 第一次往桌面添加widgets的时候才会被调用,往后在往桌面添加同类型的时候就不会被调用
*/
@Override
public void onEnabled(Context context) {
context.stopService(new Intent(context,UpdateDateTime.class));
}
}






第二,清单


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name=".MainWidget">
  <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=".UpdateDateTime" />
    </application>




第三,窗口布局文件


<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="0" 
    android:initialKeyguardLayout="@layout/main_appwidget" >
</appwidget-provider>






第四,窗口布局引用文件




<?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:background="@drawable/rectangle"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/textview"
        android:text="嗯嗯呀"/>


</LinearLayout>






第五,添加一个服务


public class UpdateDateTime extends Service {
private Timer timer;
@Override
public void onCreate() { 
super.onCreate();
timer  = new Timer();
timer.schedule(new TimerTask(){ 
@Override
public void run() { 
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String text = dateFormat.format(new Date());
   RemoteViews views = new RemoteViews(getPackageName(),R.layout.main_appwidget);
   views.setTextViewText(R.id.textview, text);
   AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
   appWidgetManager.updateAppWidget(new ComponentName(getApplicationContext(),MainWidget.class), views); 

}, 0,1000);
}


@Override
public IBinder onBind(Intent arg0) { 
return null;
}

@Override
public void onDestroy() {
timer.cancel();
timer = null;
super.onDestroy();
}


}















































































0 0