Appwidget Application (iv) The GetService of the---pendingintent

Source: Internet
Author: User

Here is an example of a service communication in Appwidget

Create a service subclass to handle commands sent by Appwidget.

[Java]View Plaincopy
  1. Package com.example.service;
  2. Import Android.app.Service;
  3. Import android.content.Intent;
  4. Import Android.os.IBinder;
  5. Public class MyService extends service{
  6. @Override
  7. Public IBinder Onbind (Intent Intent) {
  8. //TODO auto-generated method stub
  9. return null;
  10. }
  11. @Override
  12. public int Onstartcommand (Intent Intent, int flags, int startid) {
  13. //TODO auto-generated method stub
  14. System.out.println ("--------------flags--------->" + flags);
  15. System.out.println ("--------------startid--------->" + startid);
  16. System.out.println ("----------------------->onstartcommand");
  17. String value = Intent.getstringextra ("Hello");
  18. System.out.println ("value =" + value);
  19. return Super.onstartcommand (intent, flags, Startid);
  20. }
  21. }

Binds a listener to a button and sends a service when the button is clicked

[Java]View Plaincopy
  1. /**
  2. * Reached the specified update time or was called when the user added appwidget to the desktop
  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. //Create a Intent object
  10. Intent Intent = new Intent (context, MyService.   Class);
  11. Intent.putextra ("Hello", "I am a Service");
  12. //Here is getactivity, of course, it can be broadcastreceiver, etc. send a broadcast message
  13. Pendingintent pendingintent = Pendingintent.getservice (context, 0,
  14. Intent, 0);
  15. Remoteviews remoteviews = new Remoteviews (Context.getpackagename (),
  16. R.layout.appwidgetlayout);
  17. //For button binding listener
  18. Remoteviews.setonclickpendingintent (R.id.btnsend, pendingintent);
  19. //Update Appwidget
  20. Appwidgetmanager.updateappwidget (Appwidgetids, remoteviews);
  21. }


And, of course, registering the service on the androidmainfest.

[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. <uses-sdk
  7. android:minsdkversion="8"
  8. android:targetsdkversion=" /> "
  9. <application
  10. android:allowbackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/apptheme" >
  14. <activity
  15. android:name="com.example.appwidgetdemo.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="Android.intent.action.MAIN" />
  19. <category android:name="Android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </Activity>
  22. <receiver android:name="com.example.appwidgetdemo.appWidgetActivity" >
  23. <intent-filter>
  24. <action android:name="Android.appwidget.action.APPWIDGET_UPDATE" >
  25. </Action>
  26. </intent-filter>
  27. <meta-data
  28. android:name="Android.appwidget.provider"
  29. android:resource="@xml/appwidget01" />
  30. </receiver>
  31. <span style="color: #ff0000;" ><service android:name="Com.example.service.myService" >
  32. </Service></span>
  33. </Application>
  34. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.