AppWidget應用(四)---PendingIntent 之 getService

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   color   

下面給出一個appWidget中Service通訊的例子

建立一個Service子類,用來處理appWidget發送過來的命令

 

[java] view plaincopy 
  1. package com.example.service;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6.   
  7. public class myService extends Service{  
  8.   
  9.     @Override  
  10.     public IBinder onBind(Intent intent) {  
  11.         // TODO Auto-generated method stub  
  12.         return null;  
  13.     }  
  14.   
  15.     @Override  
  16.     public int onStartCommand(Intent intent, int flags, int startId) {  
  17.         // TODO Auto-generated method stub  
  18.         System.out.println("--------------flags--------->" + flags);  
  19.         System.out.println("--------------startId--------->" + startId);  
  20.         System.out.println("----------------------->onStartCommand");  
  21.         String value = intent.getStringExtra("hello");  
  22.         System.out.println("value = " + value);  
  23.         return super.onStartCommand(intent, flags, startId);  
  24.     }  
  25.   
  26. }  

為按鈕綁定監聽器,當點擊按鈕時發送一個Service

 

 

[java] view plaincopy 
  1. /** 
  2.      * 到達指定的更新時間或者當使用者向案頭添加AppWidget時被調用 
  3.      */  
  4.     @Override  
  5.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
  6.             int[] appWidgetIds) {  
  7.         System.out.println("----------------onUpdate");  
  8.         // TODO Auto-generated method stub  
  9.         // 建立一個Intent對象  
  10.         Intent intent = new Intent(context, myService.class);  
  11.         intent.putExtra("hello", "我是一個Service");  
  12.         // 這裡是getActivity,當然也可以是broadcastReceiver等   發送一個廣播訊息  
  13.         PendingIntent pendingIntent = PendingIntent.getService(context, 0,  
  14.                 intent, 0);  
  15.         RemoteViews remoteViews = new RemoteViews(context.getPackageName(),  
  16.                 R.layout.appwidgetlayout);  
  17.   
  18.         //為按鈕綁定監聽器  
  19.         remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);  
  20.   
  21.         // 更新Appwidget  
  22.         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);  
  23.     }  


當然還要在AndroidMainfest上對Service進行註冊

 

 

[html] view plaincopy 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.appwidgetdemo"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.appwidgetdemo.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.   
  26.         <receiver android:name="com.example.appwidgetdemo.appWidgetActivity" >  
  27.             <intent-filter>  
  28.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" >  
  29.                 </action>  
  30.             </intent-filter>  
  31.   
  32.             <meta-data  
  33.                 android:name="android.appwidget.provider"  
  34.                 android:resource="@xml/appwidget01" />  
  35.   
  36.         </receiver>  
  37.           
  38.         <span style="color:#ff0000;"><service android:name="com.example.service.myService" >  
  39.         </service></span>  
  40.     </application>  
  41.   
  42. </manifest>  

 

 

啟動後的效果


當點擊按鈕時會在控制台上列印:我是一個Service

當然你可以處理你感興趣的事

 

範例代碼

點擊開啟連結

 

原文:http://blog.csdn.net/deng0zhaotai/article/details/10415209

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.