安卓:从IntentService吐司保持在屏幕上永远 [英] Android: Toast from IntentService remains on screen for ever

查看:797
本文介绍了安卓:从IntentService吐司保持在屏幕上永远的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查这个问题,但它不 ŧ出庭回答我的问题,这是参与要少得多。我打电话从菜单项在我的主过程中的IntentService。它是目前只是其中飞架在onHandleIntent敬酒()最终应该在处理结束短暂出现说这是完成了骨架。然而,它仍然在屏幕永远(即使应用程序已停止)上。我用模拟器和我的银河S2具有相同的测试结果都。有人能指出我朝着正确的方向吗?

这是服务code:

 包com.enborne.spine; 进口android.app.IntentService;
 进口android.content.Intent;
 进口android.util.Log;
 进口android.widget.Toast; 公共类SAVEFILE扩展IntentService {
     私人最终字符串标记=SAVEFILE;     公共SAVEFILE(){
         超(SAVEFILE);
     }     @覆盖
     公共无效的onCreate(){
         super.onCreate();
         Log.d(TAG,服务启动。);
     }     @覆盖
     公共无效的onDestroy(){
         super.onDestroy();
         Log.d(TAG,服务被摧毁..);
     }     @覆盖
     保护无效onHandleIntent(意向意图){
         // TODO自动生成方法存根
         Log.d(TAGHandleIntent);
         //保存文件
         Toast.makeText(这一点,文件已保存,Toast.LENGTH_SHORT).show();
     }
 }

(本的onCreate和覆盖的onDestroy仅是暂时出现在Eclipse中记录,所以我可以看到发生了什么。)

我调用它从菜单中的活动因而(在与上下文搞乱只是因为服务没有启动 - 一些白痴忘了把它列入清单!):

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    我的意图;
    开关(item.getItemId()){
    案例R.id.chart:
        I =新意图(getBaseContext(),Chart.class);
        i.putExtra(KEY_CENTRE_LAT,mCentreLat); //中心坐标
        i.putExtra(KEY_CENTRE_LONG,mCentreLong); //中心坐标
        i.putExtra(KEY_RADIUS,mRadius); //有效半径
        startActivity(ⅰ);
        打破;    案例R.id.save:
        Log.d(SAVEFILE,启动服务...);
        // I =新意图(getBaseContext(),SaveFile.class);
        I =新意图(这一点,SaveFile.class);
        startService(ⅰ);
        打破;
    }
    返回true;
}

编辑:我发现这篇文章,这也许可以解释这个问题。引用一句话:


  

但是,您只能在主界面线程使用吐司,要不你遇到问题,其中吐司消息不会一段时间后消失(因为主界面背景不知道在用吐司任何消息单独的线程上下文)。



解决方案

的文章中我的编辑联系到导致我的这个博客其中固定我的问题。我没有看到其他地方,你不能从服务直接发出致祝酒辞。所以,我onHandleIntent现在看起来是这样的:

 私人处理程序处理程序;@覆盖
保护无效onHandleIntent(意向意图)
{
    Log.d(TAGonHandleIntent);    / *剥离出来的一些code在这里,后来我加入,以保持它类似
     *我上面的例子
     * /    handler.post(新的Runnable()
    {
// @覆盖
        公共无效的run()
        {
            Toast.makeText(getApplicationContext(),文件已保存,
                Toast.LENGTH_SHORT).show();
        }
    }); //显示吐司和退出
}

(如果有人可以解释为什么我不得不注释掉@覆盖上运行(),以避免错误,我将不胜感激。有我的应用程序的其他几个地方,我不得不这样做。)

编辑:我才刚刚发现这个帖子这几乎是同样的事情。我不知道我怎么之前错过了。

I checked this question, but it doesn't appear to answer my problem, which is much less involved. I am calling an IntentService from a menu item in my main process. It is currently just a skeleton which puts up a Toast in onHandleIntent() which eventually should appear briefly at the end of the processing to say it's finished. However it remains on the screen for ever (even when the application is stopped). I tested both with the emulator and my Galaxy S2 with identical results. Can someone point me in the right direction, please?

This is the service code:

 package com.enborne.spine;

 import android.app.IntentService;
 import android.content.Intent;
 import android.util.Log;
 import android.widget.Toast;

 public class SaveFile extends IntentService {
     private final String TAG = "SaveFile";

     public SaveFile() {
         super("SaveFile");
     }

     @Override
     public void onCreate() {
         super.onCreate();
         Log.d(TAG, "Service Started.. ");
     }

     @Override
     public void onDestroy() {
         super.onDestroy();
         Log.d(TAG, "Service Destroyed.. ");
     }

     @Override
     protected void onHandleIntent(Intent intent) {
         // TODO Auto-generated method stub
         Log.d(TAG, "HandleIntent");
         // File saved
         Toast.makeText(this, "File has been saved", Toast.LENGTH_SHORT).show();
     }
 }

(The onCreate and onDestroy overrides are only there temporarily for logging in Eclipse so I can see what's happening.)

I invoke it from a menu in an activity thus (the messing with context was only because the service didn't start - some idiot forgot to include it in the manifest!):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent i;
    switch (item.getItemId()) {
    case R.id.chart:
        i = new Intent(getBaseContext(), Chart.class);
        i.putExtra(KEY_CENTRE_LAT, mCentreLat); // centre coordinate
        i.putExtra(KEY_CENTRE_LONG, mCentreLong); // centre coordinate
        i.putExtra(KEY_RADIUS, mRadius); // effective radius
        startActivity(i);
        break;

    case R.id.save:
        Log.d("SaveFile", "Starting service...");
        //      i = new Intent(getBaseContext(), SaveFile.class);
        i = new Intent(this, SaveFile.class);
        startService(i);
        break;
    }
    return true;
}

EDIT: I found this article, which may explain the problem. To quote one sentence:

However, you can only use Toast in the main GUI thread, or else you run into problems where the Toast message doesn't disappear after a period (because the main GUI context doesn't know anything about Toast messages used in a separate thread context).

解决方案

The article linked to in my edit led me to this blog which fixed my problem. I had not seen elsewhere that you cannot issue a toast directly from a service. So my onHandleIntent now looks like this:

private Handler handler;

@Override
protected void onHandleIntent(Intent intent)
{
    Log.d(TAG, "onHandleIntent");

    /* Stripped out some code here that I added later to keep it similar to
     * my example above
     */

    handler.post(new Runnable()
    {  
//      @Override
        public void run()
        {
            Toast.makeText(getApplicationContext(), "File has been saved", 
                Toast.LENGTH_SHORT).show();
        }
    });    // Display toast and exit
}

(If anyone can explain why I had to comment out the @Override on the run() to avoid an error, I would be grateful. There are several other places in my application that I have had to do that.)

Edit: I have only just found this post which is virtually the same thing. I don't know how I missed it before.

这篇关于安卓:从IntentService吐司保持在屏幕上永远的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆