AppWidget应用(四)---PendingIntent 之 getService
来源:互联网 发布:剑侠情缘3mac版 编辑:程序博客网 时间:2024/06/16 09:11
下面给出一个appWidget中Service通讯的例子
创建一个Service子类,用来处理appWidget发送过来的命令
- 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);
- }
- }
- /**
- * 到达指定的更新时间或者当用户向桌面添加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进行注册
- <?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
当然你可以处理你感兴趣的事
样例代码
点击打开链接
- AppWidget应用(四)---PendingIntent 之 getService
- AppWidget应用(四)---PendingIntent 之 getService
- AppWidget应用(二)---PendingIntent 之 getActivity
- AppWidget应用(三)---PendingIntent 之 getBroadcast
- AppWidget应用(二)---PendingIntent 之 getActivity
- AppWidget应用(三)---PendingIntent 之 getBroadcast
- PendingIntent在AppWidget程序中的应用
- PendingIntent在AppWidget程序中的应用
- PendingIntent在AppWidget程序中的应用
- Android开发之AppWidget应用(一)
- Android开发之AppWidget应用(二)
- Android开发之AppWidget应用(一)
- Android开发之AppWidget应用(二)
- Android开发之AppWidget应用(一)
- AppWidget(二) PendingIntent
- AppWidget应用(一)---创建一个appWidget
- AppWidget应用(一)---创建一个appWidget
- Android:AppWidget,PendingIntent,RemoteViews用法
- AppWidget应用(三)---PendingIntent 之 getBroadcast
- MULE ESB功能介绍
- Ubuntu环境下配置J2EE开发环境jdk+tomcat+mysql+myeclipse
- oracle知识总结
- ubuntu下开源杀毒软件推荐:Clam AntiVirus
- AppWidget应用(四)---PendingIntent 之 getService
- 我的java之路——判断字符串是不是回文串
- 开源的ESB产品列表信息
- createuserortablespace
- Android系统中的消息处理Looper、Handler、Message
- wp
- MULE ESB简介
- 关于13.04的gnome3
- scanf()函数