返回列表 發帖

MyWidget

  1. package com.example.mywidget;

  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.app.PendingIntent;
  5. import android.appwidget.AppWidgetManager;
  6. import android.appwidget.AppWidgetProvider;
  7. import android.content.Context;
  8. import android.view.Menu;

  9. public class MyWidget extends AppWidgetProvider
  10. {
  11.         private PendingIntent service = null;
  12.        
  13.     @Override
  14.     public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds)
  15.     {
  16.            
  17.     }

  18.     @Override
  19.     public void onDisabled(Context context)
  20.     {
  21.         
  22.     }
  23. }
複製代碼

  1. final AlarmManager m = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
複製代碼

TOP

  1. final Date Time = new Date();
  2.                     final Intent intent = new Intent(context,MyService.class);
複製代碼

TOP

  1. service = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
複製代碼

TOP

  1. m.setRepeating(AlarmManager.RTC, Time.getTime(), 1000, service);
複製代碼

TOP

  1. final AlarmManager m = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  2.             m.cancel(service);
複製代碼

TOP

main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >
  6.     <TextView
  7.         android:id="@+id/now"
  8.         android:gravity="center"
  9.         android:textColor="@android:color/white"
  10.         android:textSize="12dp"
  11.         android:layout_width="match_parent"
  12.             android:layout_height="match_parent"
  13.         android:text="@string/init"
  14.         />

  15. </LinearLayout>
複製代碼

TOP

main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >
  6.     <TextView
  7.         android:id="@+id/now"
  8.         android:gravity="center"
  9.         android:textColor="@android:color/white"
  10.         android:textSize="12dp"
  11.         android:layout_width="match_parent"
  12.             android:layout_height="match_parent"
  13.         android:text="@string/init"
  14.         />

  15. </LinearLayout>
複製代碼

TOP

strings.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.         <string name="app_name">MyWidget</string>
  4.     <string name="init">Initializing...</string>
  5. </resources>
複製代碼

TOP

widget.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:minWidth="72dp"
  4.     android:minHeight="72dp"
  5.     android:updatePeriodMillis="0"
  6.     android:initialLayout="@layout/main" >
  7. </appwidget-provider>
複製代碼

TOP

本帖最後由 ray 於 2012-11-12 21:22 編輯
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2.     package="com.example.mywidget"
  3.     android:versionCode="1"
  4.     android:versionName="1.0" >

  5.     <uses-sdk
  6.         android:minSdkVersion="8"
  7.         android:targetSdkVersion="15" />

  8.     <application
  9.         android:icon="@drawable/ic_launcher"
  10.         android:label="@string/app_name"
  11.         android:theme="@style/AppTheme" >
  12.         <receiver
  13.             android:name=".MyWidget">
  14.             <intent-filter>
  15.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  16.             </intent-filter>
  17.             <meta-data android:name="android.appwidget.provider"
  18.                 android:resource="@xml/widget" />
  19.         </receiver>
  20.         <service android:name=".MyService">
  21.         </service>
  22.     </application>

  23. </manifest>
複製代碼

TOP

  1. package com.example.mywidget;

  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;

  5. public class MyService extends Service {
  6.     public MyService() {
  7.     }
  8.    
  9.     @Override
  10.     public int onStartCommand(Intent intent,int flags,int startId)
  11.     {
  12.             buildUpdate();
  13.             return super.onStartCommand(intent, flags, startId);
  14.     }
  15.    
  16.     private void buildUpdate()
  17.     {
  18.            
  19.     }
  20.    
  21.     @Override
  22.     public IBinder onBind(Intent intent) {
  23.         // TODO: Return the communication channel to the service.
  24.         throw new UnsupportedOperationException("Not yet implemented");
  25.     }
  26. }
複製代碼

TOP

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

TOP

  1. private void buildUpdate()
  2.     {
  3.             String lastUpdate = new SimpleDateFormat("yyyy/MM/dd\n\nE\n\na hh:mm:ss").format(new Date());
  4.     }
複製代碼

TOP

  1. RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.main);
複製代碼

TOP

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >

  6.     <TextView
  7.         android:id="@+id/now"
  8.         android:gravity="center"
  9.         android:textColor="@android:color/white"
  10.         android:textSize="12dp"
  11.         android:layout_width="fill_parent"
  12.         android:layout_height="fill_parent"
  13.         android:text="@string/init" />

  14. </LinearLayout>
複製代碼

TOP

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

TOP

  1. ComponentName thisWidget = new ComponentName(this,MyWidget.class);
  2.         AppWidgetManager manager = AppWidgetManager.getInstance(this);
  3.         manager.updateAppWidget(thisWidget,remoteView);
複製代碼

TOP

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

TOP

  1. String lastUpdate = new SimpleDateFormat("yyyy/MM/dd\n\nE\n\na hh:mm:ss").format(new Date());
  2.             RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.main);
  3.             remoteView.setTextViewText(R.id.now, lastUpdate);
  4.            
  5.         ComponentName thisWidget = new ComponentName(this,MyWidget.class);
  6.         AppWidgetManager manager = AppWidgetManager.getInstance(this);
  7.         manager.updateAppWidget(thisWidget,remoteView);
複製代碼

TOP

返回列表