Android 日历插件
来源:互联网 发布:如何查看端口是否开放 编辑:程序博客网 时间:2024/05/20 16:44
package com.wei.androidtest.widgets;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.util.Log;import android.widget.Toast;/** * 日历插件 * @author WEI * */public class CalendarWidget extends AppWidgetProvider{ @Override public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) { Log.e("widget", "--- onUpdate ---"); super.onUpdate(context, appWidgetManager, appWidgetIds); context.startService(new Intent(context, TimerService.class)); } @Override public void onDeleted(Context context, int[] appWidgetIds) { Log.e("widget", "--- onDeleted ---"); super.onDeleted(context, appWidgetIds); } // 插件被激活时启动计时service @Override public void onEnabled(Context context) { Log.e("widget", "--- onEnabled ---"); super.onEnabled(context); context.startService(new Intent(context, TimerService.class)); } // 插件被移除时停止计时service @Override public void onDisabled(Context context) { Log.e("widget", "--- onDisabled ---"); super.onDisabled(context); context.stopService(new Intent(context, TimerService.class)); } // AppWidgetProvider本身继承自BroadcastReceiver,所以可以在这里监听广播 @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); Log.e("widget", "--- onReceive ---"); String action = intent.getAction(); Log.e("action", "--- " + action + " ---"); if (action.equals("com.wei.test")) { // 点击事件 try { // 如被点击则启动系统日历 Intent intent1 = new Intent(); intent1.setComponent(new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity")); intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 如果不是在Activity内部启动Activity。则要加上此标志 context.startActivity(intent1); } catch (Exception e) { Log.e("exception : ", e.getMessage()); Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show(); } } else if (action.equals("android.appwidget.action.SCREEN_OFF")) { // 收到锁屏广播,则停止计时service。以节省电源及内存,提高性能 context.stopService(new Intent(context, TimerService.class)); } else if (action.equals(Intent.ACTION_USER_PRESENT)) { // 收到解锁广播时,则启动计时service,开始计时 context.startService(new Intent(context, TimerService.class)); } }}
package com.wei.androidtest.widgets;import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.TimeZone;import java.util.Timer;import java.util.TimerTask;import com.wei.androidtest.R;import com.wei.androidtest.utils.Lunar;import android.app.PendingIntent;import android.app.Service;import android.appwidget.AppWidgetManager;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.os.IBinder;import android.os.PowerManager;import android.util.Log;import android.widget.RemoteViews;/** * 更新时间的service * @author WEI * */public class TimerService extends Service{ Timer timer; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Log.e("service", "--- onCreate ---"); super.onCreate(); timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { try { setCurrentTime(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }, 0, 1000); } // 设置当前时间 private void setCurrentTime() throws UnsupportedEncodingException {// Log.e("service", "--- setCurrentTime ---"); long timeMillis = System.currentTimeMillis(); String date = getDate(timeMillis); Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); int weekDay = c.get(Calendar.DAY_OF_WEEK); // 该类是为了获取当前农历日期,如不需要显示农历可不用 Lunar lunar = new Lunar(c); String month = lunar.getCMonth(); String day = lunar.getCDate(); //获取Widgets管理器 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext()); //widgetManager所操作的Widget对应的远程视图即当前Widget的layout文件 RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout); remoteViews.setTextViewText(R.id.txt_currentTime, new StringBuilder() .append(pad(hour)).append(":").append(pad(minute))); remoteViews.setTextViewText(R.id.txt_second, pad(second)); remoteViews.setTextViewText(R.id.txt_date_1, date); remoteViews.setTextViewText(R.id.txt_date_2, month + "月" + day); remoteViews.setTextViewText(R.id.txt_week, parseWeekday(weekDay)); // 注册点击事件 Intent intent = new Intent("com.wei.test"); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0); remoteViews.setOnClickPendingIntent(R.id.txt_currentTime, pendingIntent); // 更新日历插件界面 ComponentName componentName = new ComponentName(getApplicationContext(), CalendarWidget.class); appWidgetManager.updateAppWidget(componentName, remoteViews); PowerManager mng = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE); if (mng.isScreenOn()) {// Log.e("service", "--- 屏幕开 ---"); } else { Log.e("service", "--- 屏幕关 ---"); Intent intent1 = new Intent("android.appwidget.action.SCREEN_OFF"); sendBroadcast(intent1); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("service", "--- onStartCommand ---" + flags + startId); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e("service", "--- onDestroy ---"); timer.cancel(); timer = null; super.onDestroy(); } public String pad(int c) { if (c >= 10) return String.valueOf(c); else return "0" + String.valueOf(c); } public String getDate(long time) throws UnsupportedEncodingException { SimpleDateFormat format = new SimpleDateFormat("MM月dd日"); return format.format(new Date(time)); } public String parseWeekday(int day) throws UnsupportedEncodingException { String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; return weekDays[day - 1]; }}
在res目录下新建xml文件夹,并新建文件:widget_rovider.xml:
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="460dp" android:minHeight="180dp" android:updatePeriodMillis = "1000" android:initialLayout="@layout/widget_layout"></appwidget-provider>
注:android:initialLayout指明日历插件要使用的布局,也就是插件要做成什么样子,皆由widget_layout.xml决定。
再在layout目录下新建布局文件:widget_layout.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="200dp" android:layout_height="200dp" android:layout_marginRight="20dp" android:background="@android:color/transparent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/txt_currentTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:text="13:50" android:textColor="#dd434d" android:textSize="50sp" android:textStyle="bold" /> <TextView android:id="@+id/txt_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:text="50" android:textColor="#dd434d" android:textSize="20sp" android:textStyle="bold" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:gravity="center" android:orientation="vertical" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" > <TextView android:id="@+id/txt_date_1" style="@style/Normal_Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:padding="5dp" android:text="9月24日" /> <TextView android:id="@+id/txt_week" style="@style/Normal_Text" android:layout_toRightOf="@id/txt_date_1" android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:padding="5dp" android:text="星期四" /> </RelativeLayout> <TextView android:id="@+id/txt_date_2" style="@style/Normal_Text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:gravity="center_vertical" android:paddingLeft="5dp" android:text="八月十四" /> </LinearLayout> </LinearLayout> </LinearLayout></LinearLayout>
<application android:persistent="true" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name=".widgets.CalendarWidget" android:label="@string/app_name" android:icon="@drawable/bg_clock"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.PACKAGE_RESTARTED"/> <action android:name="android.intent.action.USER_PRESENT"/> <!-- 解锁 --> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> <action android:name="android.appwidget.action.SCREEN_OFF"/> <action android:name="com.wei.test"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider"/> </receiver> </application>
若不想app被小米或360等一键清理给干掉,需配置android:persistent="true"
并把apk放到system/app目录下,让我们的日历桌面插件成为系统应用,再重启手机。这样我们的桌面插件app就不会被第三方app给一键清理掉。
1 1
- Android 日历插件
- android日历多选 日历插件
- 日历插件
- 日历插件
- 日历插件
- 日历插件
- 日历插件
- 日历插件
- 日历插件
- 日历插件
- 日历插件
- 日历插件
- My97日历插件
- js日历插件:My97DatePicker
- jQuery.datePicker日历插件
- Calendars - 日历插件
- jquery插件日历
- jQuery 日历插件
- session学习
- 对Objective-C中Block的追探
- localhost与127.0.0.1的区别
- log4j配置祥解
- 排列与组合算法
- Android 日历插件
- centOS lnmp操作
- ASP.NET连接sql2008数据库
- linux驱动调试技巧:灌寄存器---------以mma7660为例
- 文章标题
- iOS异常处理
- Android FDN开启后手机仍然能够上网
- Spring MVC实现CRM系统架构
- Spring和spring MVC,spring MVC和Strut2的区别