Solutions for using Toast to display problems in the Android service _android

Source: Internet
Author: User
Tags message queue sqlite database

The example in this article describes the solution for using toast in the Android service to display problems correctly. Share to everyone for your reference, specific as follows:

In the service of simple practice, oncreate, OnStart, OnDestroy three methods are called Toast.maketext in the same way as in the activity, and the OnStart and OnDestroy methods of the service are invoked through two buttons in Acitivy:

The Demoservice code is as follows:

@Override public
void OnCreate ()
{
    super.oncreate ();
    Toast.maketext (Getapplicationcontext (), Service is created!, Toast.length_long). Show ();
@Override public
void OnStart (Intent intent,int startid)
{
    Super.onstart (Intent, startid);
    Toast.maketext (Getapplicationcontext (), Service is on!, Toast.length_long). Show ();
@Override public
void OnDestroy () {
    Super.ondestroy ();
    Toast.maketext (Getapplicationcontext (), Service is off!, Toast.length_long). Show ();


After running, the information in the Demoservice is not displayed.

Just start to think that the context is not correct, in the service directly call Getapplicationcontext () is the context of the service, but in the light of the view, toast should be the main UI to display, So looking for a moment, Google's note to toast, there is a sentence:

"A toast can be created and displayed from the activity or Service." If you create a toast notification from a service,it appears in front to the activity currently in focus.
(http://developer.Android.com/guide/topics/ui/notifiers/toasts.html)

So according to this sentence, the toast created in the service will be highlighted in front of the Acivity UI. But why does it run without effect? Check out the Maketext method again.

Sure enough, the context of the problem, so want to toast can work properly, need to be in the activity of the main thread to run on the line, then how to get the main thread UI? You can run a custom thread on top of the main thread by handler.

Let's look at the src of the Toast.show method:

public void Show () {
    ...
    Service.enqueuetoast (Pkg, TN, mduration);  Insert the toast into a message queue
    ...
}

In principle, Android is largely a message queue and message loop, and the main thread gets the message from the message queue and processes it. and handler as a tool class for inserting messages into message queues. So we reconstructed the original code:

@Override public
void OnCreate ()
{
    super.oncreate ();
    Handler=new handler (Looper.getmainlooper ());
    Handler.post (New Runnable () {public
      void run () {
        toast.maketext (Getapplicationcontext (), Service is created! , Toast.length_long). Show ();
}} @Override public
void OnStart (Intent intent,int startid)
{
    Super.onstart (Intent, startid);
    Handler=new handler (Looper.getmainlooper ());
    Handler.post (New Runnable () {public
      void run () {
        toast.maketext (Getapplicationcontext (), Service is on!, Toast.length_long). Show ();
}} @Override public
void OnDestroy () {
    Super.ondestroy ();
    Handler=new handler (Looper.getmainlooper ());
    Handler.post (New Runnable () {public
      void run () {
        toast.maketext (Getapplicationcontext (), Service is off!, Toast.length_long). Show ();}}


The effect after running is as follows:

Summary: using toast in the Android framework will add toast to the main thread to work properly.

More interested readers of Android-related content can view the site: Android Development Primer and Advanced tutorials, the Android View View tips Summary, the activity tips summary of Android programming, Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

Related Article

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.