Android Service
List all the problems encountered when implementing Android Service
#Issue 1
PendingIntent - This is mostly used in notification to fire the intent if the user clicks on the notification. (Example: SMS Messages)
Here is one issue I encountered while using PendingIntent:
"Not getting the extras after firing the intent as shown in the example"
Solution
========
Add
FLAG_UPDATE_CURRENT
#ISSUE 2
Service Callback
Actually this is not a problem and I think callbacks were handled quite well in Android service.
This is just a quick tutorial on using the service callbacks.
Steps
1. Create your callback AIDL file.
oneway means that the service invocations does not need to wait for a return value. (No blocking)
I assume that you have your main AIDL file. This is use so you can call interact with your service.
Here you need to add your register and unregister functions.
Step 2:
Create your Service by extending service
Step 3:
Create your ArrayList that will hold all the registered classes that wants
to receive your callback messages.
In SampleService.java
Add your register and unregister functions
Step 4:
Create a function to implement your callback
In SampleService.java
Just call this function on your service if you want to send messages to your registered classes.
Step 5:
Now all you have to do is register your class
In your main class (Activity).
I register after I receive an instance of your service class
Make sure you bind your main AIDL.
OnResume Function - bind your service
OnPause Function - you can unbind your service
Add unimplemented function in your main
Final Step:
Unregister
OnPause or OnDestroy - you can unregister so you will not receive messages anymore
That's it.
#Issue 1
PendingIntent - This is mostly used in notification to fire the intent if the user clicks on the notification. (Example: SMS Messages)
Here is one issue I encountered while using PendingIntent:
"Not getting the extras after firing the intent as shown in the example"
Solution
========
Add
FLAG_UPDATE_CURRENT
#ISSUE 2
Service Callback
Actually this is not a problem and I think callbacks were handled quite well in Android service.
This is just a quick tutorial on using the service callbacks.
Steps
1. Create your callback AIDL file.
oneway means that the service invocations does not need to wait for a return value. (No blocking)
I assume that you have your main AIDL file. This is use so you can call interact with your service.
Here you need to add your register and unregister functions.
Step 2:
Create your Service by extending service
Step 3:
Create your ArrayList that will hold all the registered classes that wants
to receive your callback messages.
In SampleService.java
Add your register and unregister functions
Step 4:
Create a function to implement your callback
In SampleService.java
Just call this function on your service if you want to send messages to your registered classes.
Step 5:
Now all you have to do is register your class
In your main class (Activity).
I register after I receive an instance of your service class
Make sure you bind your main AIDL.
OnResume Function - bind your service
OnPause Function - you can unbind your service
Add unimplemented function in your main
Final Step:
Unregister
OnPause or OnDestroy - you can unregister so you will not receive messages anymore
That's it.
コメント
コメントを投稿