a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

android實現widget時鐘示例分享

2020-04-11 11:52:40
字體:
來源:轉載
供稿:網友

一、在 AndroidManifest.xml文件中配置Widgets:

復制代碼 代碼如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.widget"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".TimeWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/timewidget_info" />
        </receiver>
        <service android:name=".TimerService"></service>
    </application>
</manifest>

二、在項目的res目錄下建立xml目錄,并且創建 timewidget_info.xml 文件,內容如下:

復制代碼 代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/time_appwidget"
    android:minHeight="40dp"
    android:minWidth="40dp"
    android:updatePeriodMillis="0" />

三、在layout文件夾下建立文件time_appwidget.xml:

復制代碼 代碼如下:

<?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="wrap_content"
        android:id="@+id/textView"
        android:text="current time"
        />
</LinearLayout>

四、在drawable文件夾下建立rectangle.xml文件(這部可以省略,主要是為了美化TextView控件,漸變效果):

復制代碼 代碼如下:

<?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:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
    <stroke
        android:dashGap="1dp"
        android:dashWidth="10dp"
        android:width="6dp"
        android:color="#0000FF" />
</shape>

五、后臺代碼實現:

復制代碼 代碼如下:

package com.example.widget;

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) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
    //當一個Widgets時會被調用
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
    }
    //第一次往桌面添加Widgets時會被調用,之后添加同類型Widgets不會被調用
    public void onEnabled(Context context) {
        context.startService(new Intent(context, TimerService.class));
    }
    //從桌面上刪除最后一個Widgets時會被調用
    public void onDisabled(Context context) {
        context.stopService(new Intent(context, TimerService.class));
    }
}

復制代碼 代碼如下:

package com.example.widget;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
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 TimerService extends Service {
    private Timer timer;

    @Override
    public void onCreate() {   
        super.onCreate();
        timer = new Timer();
        timer.schedule(new MyTimerTask(), 0, 1000);
    }
    private final class MyTimerTask extends TimerTask{
        @SuppressLint("SimpleDateFormat")
        @Override
        public void run() {
            SimpleDateFormat sdf= new SimpleDateFormat("hh:mm:ss");
            String time = sdf.format(new Date());
            //獲取Widgets管理器
              AppWidgetManager widgetManager =AppWidgetManager.getInstance(getApplicationContext());
            //widgetManager所操作的Widget對應的遠程視圖即當前Widget的layout文件
              RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.time_appwidget);
            remoteView.setTextViewText(R.id.textView, time);
            //當點擊Widgets時觸發的世界
            //remoteView.setOnClickPendingIntent(viewId, pendingIntent)
            ComponentName componentName = new  ComponentName(getApplicationContext(),TimeWidgetProvider.class);
            widgetManager.updateAppWidget(componentName, remoteView);
        }
    }
    @Override
    public void onDestroy() {
        timer.cancel();
        timer=null;
        super.onDestroy();
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩成人一区二区 | 日韩最新网址 | 免费观看一级特黄欧美大片 | 青草青草 | 欧美a网 | 久草一区 | 久久人人爽视频 | 一级一级一级一级毛片 | 神马九九| 久久91| 欧美综合在线观看 | 国产精品毛片一区二区在线看 | 国产精品久久久久久吹潮 | 午夜免费视频 | 亚洲性免费视频 | 日日综合 | 狠狠躁夜夜躁人人爽天天高潮 | 黄色网址av | 男女网站 | 色av综合 | 一区二区三区四区在线 | 凹凸日日摸日日碰夜夜爽孕妇 | 日本高清在线观看 | 色婷婷久久久swag精品 | 亚洲国产精品av | 国产精品视频久久久久 | 新99热| 一区二区视频网站 | 国产精品99久久久久久www | 亚洲欧美日韩国产综合 | 成人久久久 | 日韩毛片在线免费观看 | 国产精品久久久久久久久免费高清 | 欧美日韩视频在线观看一区 | 午夜精品久久久久久久久久久久 | 久久伊人久久 | 青青青草视频 | 精品国产不卡一区二区三区 | 日韩一区在线视频 | 欧美精品在线一区二区三区 | 99精品欧美一区二区三区 |