標題:
MyWidget
[打印本頁]
作者:
ray
時間:
2012-11-12 20:32
標題:
MyWidget
package com.example.mywidget;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.view.Menu;
public class MyWidget extends AppWidgetProvider
{
private PendingIntent service = null;
@Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds)
{
}
@Override
public void onDisabled(Context context)
{
}
}
複製代碼
作者:
ray
時間:
2012-11-12 20:35
final AlarmManager m = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
複製代碼
作者:
ray
時間:
2012-11-12 20:40
final Date Time = new Date();
final Intent intent = new Intent(context,MyService.class);
複製代碼
作者:
ray
時間:
2012-11-12 20:45
service = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
複製代碼
作者:
ray
時間:
2012-11-12 20:50
m.setRepeating(AlarmManager.RTC, Time.getTime(), 1000, service);
複製代碼
作者:
ray
時間:
2012-11-12 20:52
final AlarmManager m = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
m.cancel(service);
複製代碼
作者:
ray
時間:
2012-11-12 21:01
main.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" >
<TextView
android:id="@+id/now"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="12dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/init"
/>
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-11-12 21:02
main.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" >
<TextView
android:id="@+id/now"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="12dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/init"
/>
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-11-12 21:03
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyWidget</string>
<string name="init">Initializing...</string>
</resources>
複製代碼
作者:
ray
時間:
2012-11-12 21:04
widget.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/main" >
</appwidget-provider>
複製代碼
作者:
ray
時間:
2012-11-12 21:19
本帖最後由 ray 於 2012-11-12 21:22 編輯
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mywidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".MyWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget" />
</receiver>
<service android:name=".MyService">
</service>
</application>
</manifest>
複製代碼
作者:
ray
時間:
2012-11-12 21:29
package com.example.mywidget;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
public MyService() {
}
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
buildUpdate();
return super.onStartCommand(intent, flags, startId);
}
private void buildUpdate()
{
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
複製代碼
作者:
ray
時間:
2012-11-12 21:36
[attach]625[/attach]
作者:
ray
時間:
2012-11-14 18:46
private void buildUpdate()
{
String lastUpdate = new SimpleDateFormat("yyyy/MM/dd\n\nE\n\na hh:mm:ss").format(new Date());
}
複製代碼
作者:
ray
時間:
2012-11-14 19:11
RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.main);
複製代碼
作者:
ray
時間:
2012-11-14 19:28
<?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:orientation="vertical" >
<TextView
android:id="@+id/now"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="12dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/init" />
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-11-14 19:38
[attach]626[/attach]
作者:
ray
時間:
2012-11-14 20:03
ComponentName thisWidget = new ComponentName(this,MyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget,remoteView);
複製代碼
作者:
ray
時間:
2012-11-14 20:04
[attach]627[/attach]
作者:
ray
時間:
2012-11-14 20:23
String lastUpdate = new SimpleDateFormat("yyyy/MM/dd\n\nE\n\na hh:mm:ss").format(new Date());
RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.main);
remoteView.setTextViewText(R.id.now, lastUpdate);
ComponentName thisWidget = new ComponentName(this,MyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget,remoteView);
複製代碼
作者:
ray
時間:
2012-11-14 21:01
[attach]628[/attach]
作者:
ray
時間:
2012-11-14 21:05
[attach]629[/attach]
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2