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


    下面给出一个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

  • 相关阅读:
    基因疗法 中国科学家利用基因疗法成功逆转1型糖尿病进程
    麻省理工比特币
    数据处理包dplyr的函数
    资源革命
    翻译文章第六章4-7
    kaggle Titanic心得
    Javascript高级编程学习笔记(56)—— DOM2和DOM3(8)低版本IE范围
    Javascript高级编程学习笔记(55)—— DOM2和DOM3(7)操作范围
    Javascript高级编程学习笔记(54)—— DOM2和DOM3(6)范围选择
    Javascript高级编程学习笔记(53)—— DOM2和DOM3(5)遍历
  • 原文地址:https://www.cnblogs.com/veins/p/3831782.html
Copyright © 2020-2023  润新知