我希望服务在进入手机主菜单、连接服务器并且如果得到消息显示自定义吐司后工作。我做了大部分,但我只能在吐司中显示文本。我想用图像制作自定义吐司,我找到了很多解决方案如何在活动中做到这一点,但它在服务中不起作用。
有人能告诉我我应该改变什么才能使这段代码正常工作吗?
    public class MyService extends Service {
    private Toast toast;
    private Timer timer;
    private TimerTask timerTask;
    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            showToast();
        }
    }
    private void showToast() {
        LayoutInflater inflater = (LayoutInflater)
           getSystemService(LAYOUT_INFLATER_SERVICE);
         View layout = inflater.inflate(R.layout.toast, null);
         ImageView image = (ImageView)
           layout.findViewById(R.id.image);
         image.setImageResource(R.drawable.truck); 
         TextView textView = (TextView)
           layout.findViewById(R.id.text);
         textView.setText("Some toast message");
         toast = new Toast(getApplicationContext());
         toast.setGravity(Gravity.BOTTOM, 0, 0);
         toast.setDuration(Toast.LENGTH_LONG);
         toast.setView(layout);
         toast.show();
    }
    @Override
    public void onCreate() {
        super.onCreate();
        timer = new Timer();
        toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        clearTimerSchedule();
        initTask();
        timer.scheduleAtFixedRate(timerTask, 4 * 1000, 4 * 1000);
        return super.onStartCommand(intent, flags, startId);
    }
    private void clearTimerSchedule() {
        if(timerTask != null) {
            timerTask.cancel();
            timer.purge();
        }
    }
    private void initTask() {
        timerTask = new MyTimerTask();
    }
    @Override
    public void onDestroy() {
        clearTimerSchedule();
        super.onDestroy();
    }
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}