日歷顯示讀出輸出的年代的java代碼。本站提示廣大學習愛好者:(日歷顯示讀出輸出的年代的java代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是日歷顯示讀出輸出的年代的java代碼正文
1、在 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>
2、在項目標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" />
3、在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>
4、在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>
5、後台代碼完成:
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;
}
}
updateNotification.defaults = Notification.DEFAULT_SOUND;//鈴聲提示
updateNotification.setLatestEventInfo(UpdateService.this, "上海地鐵", "下載完成,點擊裝置。", updatePendingIntent);
updateNotificationManager.notify(0, updateNotification);
//停滯辦事
stopService(updateIntent);
case DOWNLOAD_FAIL:
//下載掉敗
updateNotification.setLatestEventInfo(UpdateService.this, "上海地鐵", "下載完成,點擊裝置。", updatePendingIntent);
updateNotificationManager.notify(0, updateNotification);
default:
stopService(updateIntent);
}
}
};
至此,文件下載而且在告訴欄告訴進度。
發明自己空話許多,其實幾句話的工作,來往返回寫了這麼多,煩瑣了,前面博文我會朝著精簡方面盡力。
PS:後面說要附上cheanUpdateFile()的代碼
File updateFile = new File(Global.downloadDir,getResources().getString(R.string.app_name)+".apk");
if(updateFile.exists()){
//當不須要的時刻,消除之前的下載文件,防止糟蹋用戶空間
updateFile.delete();
}