吐司以自运行的服务 [英] Toast in a self running service

查看:120
本文介绍了吐司以自运行的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它运行一个远程服务,然后退出的机器人活动。 该服务本身,轮询一个设备节点上,并检查有无变化, 我想用面包来提醒用户,但我没有奶源才能正常工作。敬酒未显示,过了一会,Android的呼喊,我的应用程序没有响应。 顺便说一句,我不想​​再次启动活动,并显示来自那里的敬酒,我只是希望它弹出显示给用户当前屏幕上。

下面是服务code:

 公共类MainService延伸服务{

    //本地方法
    公共本地静态INT GetWiegand code();
    公共本地静态无效openWiegand();
    公共本地静态无效closeWiegand();

    静态INT code = 0;

    // 其他
    私有静态最后字符串变量= MainService.class.getSimpleName();
    私人处理程序处理程序;

    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }

    公共无效的run(){
        处理程序H;
        而(真){
            code = GetWiegand code();
            如果(code> 0){
                H =新的处理程序(this.getMainLooper());
                h.post(新的Runnable(){
                    @覆盖
                    公共无效的run(){
                        Toast.makeText(getBaseContext(),
                            ID+ Integer.toString(code)+
                            刚刚进入,Toast.LENGTH_LONG).show();
                    }
                });
            }
        }
    }

    @覆盖
    公共无效的onCreate(){
        super.onCreate();
        openWiegand();
        Log.i(TAG,服务启动);
        this.run();
    }

    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
        closeWiegand();
        Log.i(TAG,服务破坏);
    }

    静态{
        的System.loadLibrary(韦根 - 土司);
    }
}
 

解决方案

您不能调用从服务敬酒消息。除了从UI线程你不能这样做与UI什么。你将需要研究的许多方面与您服务您的UI线程通信的一个 - BroadcastReciever,信使,AIDL等

有关你想做什么,你也许并不需要尽量去为AIDL路线。看看这个例子的使者执行,然后检查出完整的示例从SDK样本:

<一个href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.html" rel="nofollow">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.html

I have an android activity which runs a remote service and then quits. The service itself, polls on a device node, and checks for changes, I want to use toast to alert the user, but I didn't mange to get it to work. the Toast is not showing, and after a while, Android shouts that my app is not responding. BTW, I don't want to start the activity again and display the toast from there, I just want it to pop on the current screen shown to the user.

Here is the service code:

public class MainService extends Service {

    // Native methods
    public native static int GetWiegandCode();
    public native static void openWiegand();
    public native static void closeWiegand();

    static int code = 0;

    // Other
    private static final String TAG = MainService.class.getSimpleName();
    private Handler handler;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void run() {
        Handler h;
        while (true) {
            code = GetWiegandCode();
            if (code > 0) {
                h = new Handler(this.getMainLooper());
                h.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(),
                            "ID " + Integer.toString(code) +
                            "Just entered", Toast.LENGTH_LONG).show();
                    }
                });
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        openWiegand();
        Log.i(TAG, "Service Starting");
        this.run();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        closeWiegand();
        Log.i(TAG, "Service destroying");
    }

    static {
        System.loadLibrary("wiegand-toast");
    }
}

解决方案

You can't call a Toast message from the Service. You can't do anything with the UI except from the UI thread. You're going to need to research one of the many ways to communicate with your UI thread from your service - BroadcastReciever, Messenger, AIDL, etc.

For what you're trying to do, you probably don't need to go as far as the AIDL route. Check out this example the Messenger implementation and then check out the complete example from the sdk-samples:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.html

这篇关于吐司以自运行的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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