android實(shí)現(xiàn)widget時(shí)鐘示例分享
一、在 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>
二、在項(xiàng)目的res目錄下建立xml目錄,并且創(chuàng)建 timewidget_info.xml 文件,內(nèi)容如下:
<?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" >
<!-- 設(shè)置弧度 -->
<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>
五、后臺(tái)代碼實(shí)現(xiàn):
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);
}
//當(dāng)一個(gè)Widgets時(shí)會(huì)被調(diào)用
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}
//第一次往桌面添加Widgets時(shí)會(huì)被調(diào)用,之后添加同類型Widgets不會(huì)被調(diào)用
public void onEnabled(Context context) {
context.startService(new Intent(context, TimerService.class));
}
//從桌面上刪除最后一個(gè)Widgets時(shí)會(huì)被調(diào)用
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對(duì)應(yīng)的遠(yuǎn)程視圖即當(dāng)前Widget的layout文件
RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.time_appwidget);
remoteView.setTextViewText(R.id.textView, time);
//當(dāng)點(diǎn)擊Widgets時(shí)觸發(fā)的世界
//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;
}
}
- Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法
- Android多功能時(shí)鐘開(kāi)發(fā)案例(實(shí)戰(zhàn)篇)
- Android 仿日歷翻頁(yè)、仿htc時(shí)鐘翻頁(yè)、數(shù)字翻頁(yè)切換效果
- Android多功能時(shí)鐘開(kāi)發(fā)案例(基礎(chǔ)篇)
- android高仿小米時(shí)鐘(使用Camera和Matrix實(shí)現(xiàn)3D效果)
- Android實(shí)現(xiàn)簡(jiǎn)單時(shí)鐘View的方法
- Android自定義動(dòng)態(tài)壁紙開(kāi)發(fā)(時(shí)鐘)
- Android編程基于自定義控件實(shí)現(xiàn)時(shí)鐘功能的方法
- Android仿小米時(shí)鐘效果
- Android自定義View實(shí)現(xiàn)時(shí)鐘功能
相關(guān)文章
Flutter TV Android端開(kāi)發(fā)技巧詳細(xì)教程
這篇文章主要為大家介紹了Flutter TV Android端開(kāi)發(fā)技巧詳細(xì)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12android通過(guò)配置文件設(shè)置應(yīng)用安裝到SD卡上的方法
在AndroidManifest.xml文件的manifest里面加上一句話,就可以把應(yīng)用安裝到SD卡上2013-11-11學(xué)習(xí)Android Handler消息傳遞機(jī)制
這篇文章主要為大家詳細(xì)介紹了Android Handler消息傳遞機(jī)制,感興趣的小伙伴們可以參考一下2016-08-08Android 3D滑動(dòng)菜單完全解析 Android實(shí)現(xiàn)推拉門式的立體特效
這篇文章主要為大家詳細(xì)介紹了Android 3D滑動(dòng)菜單,Android實(shí)現(xiàn)推拉門式的立體特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11淺析Android App的相對(duì)布局RelativeLayout
這篇文章主要介紹了Android App的相對(duì)布局RelativeLayout,文中舉了一個(gè)登錄界面的XML布局例子,非常直觀,需要的朋友可以參考下2016-04-04Android Socket服務(wù)端與客戶端用字符串的方式互相傳遞圖片的方法
這篇文章主要介紹了Android Socket服務(wù)端與客戶端用字符串的方式互相傳遞圖片的方法的相關(guān)資料,需要的朋友可以參考下2016-05-05Android利用代碼控制設(shè)備上其他音樂(lè)播放器的方法
這篇文章主要給大家介紹了關(guān)于Android利用代碼如何控制設(shè)備上其他音樂(lè)播放器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06Android自定義控件實(shí)現(xiàn)隨手指移動(dòng)的小球
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)隨手指移動(dòng)的小球,隨著手指觸摸移動(dòng)而移動(dòng)的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android Listview上下拉動(dòng)刷新tab滑動(dòng)切換功能
這篇文章主要介紹了Android Listview上下拉動(dòng)刷新tab滑動(dòng)切換功能的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04