標籤:android style blog http java color
下面給出一個appWidget中Service通訊的例子
建立一個Service子類,用來處理appWidget發送過來的命令
[java] view plaincopy
- package com.example.service;
-
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
-
- public class myService extends Service{
-
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // TODO Auto-generated method stub
- System.out.println("--------------flags--------->" + flags);
- System.out.println("--------------startId--------->" + startId);
- System.out.println("----------------------->onStartCommand");
- String value = intent.getStringExtra("hello");
- System.out.println("value = " + value);
- return super.onStartCommand(intent, flags, startId);
- }
-
- }
為按鈕綁定監聽器,當點擊按鈕時發送一個Service
[java] view plaincopy
- /**
- * 到達指定的更新時間或者當使用者向案頭添加AppWidget時被調用
- */
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- System.out.println("----------------onUpdate");
- // TODO Auto-generated method stub
- // 建立一個Intent對象
- Intent intent = new Intent(context, myService.class);
- intent.putExtra("hello", "我是一個Service");
- // 這裡是getActivity,當然也可以是broadcastReceiver等 發送一個廣播訊息
- PendingIntent pendingIntent = PendingIntent.getService(context, 0,
- intent, 0);
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
- R.layout.appwidgetlayout);
-
- //為按鈕綁定監聽器
- remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);
-
- // 更新Appwidget
- appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
- }
當然還要在AndroidMainfest上對Service進行註冊
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.appwidgetdemo"
- 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" >
- <activity
- android:name="com.example.appwidgetdemo.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- <receiver android:name="com.example.appwidgetdemo.appWidgetActivity" >
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" >
- </action>
- </intent-filter>
-
- <meta-data
- android:name="android.appwidget.provider"
- android:resource="@xml/appwidget01" />
-
- </receiver>
-
- <span style="color:#ff0000;"><service android:name="com.example.service.myService" >
- </service></span>
- </application>
-
- </manifest>
啟動後的效果
當點擊按鈕時會在控制台上列印:我是一個Service
當然你可以處理你感興趣的事
範例代碼
點擊開啟連結
原文:http://blog.csdn.net/deng0zhaotai/article/details/10415209