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

下面给出一个appWidget中Service通讯的例子

创建一个Service子类,用来处理appWidget发送过来的命令

[java]  view plain copy
  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 plain copy
  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 plain copy
  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

当然你可以处理你感兴趣的事


样例代码

点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值