Here is an example of a service communication in Appwidget
Create a service subclass to handle commands sent by 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);
- }
- }
Binds a listener to a button and sends a service when the button is clicked
[Java]View Plaincopy
- /**
- * Reached the specified update time or was called when the user added appwidget to the desktop
- */
- @Override
- public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager,
- int[] appwidgetids) {
- System.out.println ("----------------onUpdate");
- //TODO auto-generated method stub
- //Create a Intent object
- Intent Intent = new Intent (context, MyService. Class);
- Intent.putextra ("Hello", "I am a Service");
- //Here is getactivity, of course, it can be broadcastreceiver, etc. send a broadcast message
- Pendingintent pendingintent = Pendingintent.getservice (context, 0,
- Intent, 0);
- Remoteviews remoteviews = new Remoteviews (Context.getpackagename (),
- R.layout.appwidgetlayout);
- //For button binding listener
- Remoteviews.setonclickpendingintent (R.id.btnsend, pendingintent);
- //Update Appwidget
- Appwidgetmanager.updateappwidget (Appwidgetids, remoteviews);
- }
And, of course, registering the service on the androidmainfest.
[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=" /> "
- <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>
The effect after startup
When the button is clicked, it is printed on the console: I am a service
Of course you can handle what you're interested in.
Sample code
Click to open link
Original: http://blog.csdn.net/deng0zhaotai/article/details/10415209