一開始service未啟動
按按鈕啟動service
再按按鈕顯示時間
android appwidget透過一個按鈕來啟動背景servicey,在透過另一按鈕更新時間(appwidget的建立可參考上一篇)
1.
reciver註冊action (給按鈕1)Clock_widget
<action android:name="test.widget_clock.CLICK1" />
service註冊action(給按鈕2)Myservice
<action android:name="test.widget_clock.CLICK1" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.Clock_widget1"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".Clock_widget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="test.widget_clock.CLICK2" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/clockprovider" />
</receiver>
<service android:enabled="true" android:name=".Clock_widget$MyService">
<intent-filter>
<action android:name="test.widget_clock.CLICK1" />
</intent-filter>
</service>
</application>
</manifest>
2.
//綁定service,注意androidmanifest
receiver需註冊action
Intent intent1 = new Intent(context, MyService.class);
//啟動服務
context.startService(intent1);
Intent intent1 = new Intent(context, MyService.class);
//啟動服務
context.startService(intent1);
action動作的溝通需建立Intent
Clock_widget.java
package test.Clock_widget1;
import java.util.Calendar;
import java.util.Date;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.widget.RemoteViews;
import android.widget.Toast;
public class Clock_widget extends AppWidgetProvider {
//變數宣告
public Handler c_Handler;
public Handler r_Handler;
public Calendar c_Calendar; //日曆
public static String CLICK_ACTION1 ;
public static String CLICK_ACTION2 ;
public Clock_widget() {
//按鈕1字串
CLICK_ACTION1 = "test.widget_clock.CLICK1";
//按鈕2字串
CLICK_ACTION2 = "test.widget_clock.CLICK2";
c_Handler=new Handler();
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
final String action = intent.getAction();
if(action.equals(CLICK_ACTION2)){
//if(c_Thread.isInterrupted())c_Thread.resume();
//綁定service
Intent intent1 = new Intent(context, MyService.class);
//啟動服務
context.startService(intent1);
Toast.makeText(context, "開時service", Toast.LENGTH_LONG).show();
}
}
@Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//綁定Clock_widget注意androidmanifest receiver需註冊action
final Intent refreshIntent1 = new Intent(context, Clock_widget.class);
//set action字串
refreshIntent1.setAction(Clock_widget.CLICK_ACTION2);
final PendingIntent refreshPendingIntent1 = PendingIntent.getBroadcast(context, 0,refreshIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
//final PendingIntent refreshPendingIntent = PendingIntent.getService(cc, 0,refreshIntent, 0);
RemoteViews updateViews1 = new RemoteViews( context.getPackageName(), R.layout.main);
//設定button
updateViews1.setOnClickPendingIntent(R.id.button2, refreshPendingIntent1);
//update view
appWidgetManager.updateAppWidget(appWidgetIds, updateViews1);
}//onupdate
public static class MyService extends Service {
@Override
public void onStart(Intent intent, int startId) {
ComponentName thisWidget = new ComponentName(this, Clock_widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.main);
//intent.getAction()取得action字串
if (CLICK_ACTION1.equals(intent.getAction())) {
Toast.makeText(this, "開時時間", Toast.LENGTH_LONG).show();
remoteViews.setTextViewText(R.id.textView1, new Date().toLocaleString());
}else{
remoteViews.setTextViewText(R.id.textView1, "時間顯示器");
}
//綁定Clock_widget注意androidmanifest receiver需註冊action
Intent clickIntent = new Intent();
clickIntent.setAction(CLICK_ACTION1);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, clickIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent);
manager.updateAppWidget(thisWidget, remoteViews);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
}
沒有留言:
張貼留言