未传递给类的值 - 无法解析符号

时间:2017-12-11 05:01:55

标签: android

我在服务中的onstartcommand中声明了值。 然后我无法将它们传递给run方法中的displayedText。任何解决方法?

  1. 也许是为了优化时间任务?
  2. 声明全局无法完成​​,因为不允许使用公共修饰符。 此外,如果我重新定义displayedText,它将不会传递来自displayedText = intent.getStringExtra的任何值(" PassToService");
  3. MyService.java

    public class MyService extends Service {
    public static final long INTERVAL=3000;//variable to execute services every 5 second
    private Handler mHandler=new Handler(); // run on another Thread to avoid crash
    private Timer mTimer=null; // timer handling
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("unsupported Operation");
    }
    @Override
    public void onCreate() {
    
    }
    
    private String displayingText;
    
    public int onStartCommand (Intent intent, int flags, int startId) {
        String displayingText = intent.getStringExtra("PassToService");
        Toast.makeText(this, displayingText, Toast.LENGTH_SHORT).show();
    
        mTimer=new Timer(); // recreate new timer
        mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0,INTERVAL);
    
        //return super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }
    
    
    @Override
    public void onTaskRemoved(Intent rootIntent) {
        stopSelf();///its will stop service
        super.onTaskRemoved(rootIntent);
    }
    @Override
    public void onDestroy() {
        Toast.makeText(this, "In Destroy", Toast.LENGTH_SHORT).show();//display toast when method called
        mTimer.cancel();//cancel the timer
        super.onDestroy();
    
    }
    
    //inner class of TimeDisplayTimerTask
    private class TimeDisplayTimerTask extends TimerTask {
        @Override
        public void run() {
    
            // run on another thread
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    // display toast at every 10 second
                    final Toast Notify = Toast.makeText(getApplicationContext(), displayingText, Toast.LENGTH_SHORT);
                    Notify.setGravity(Gravity.CENTER, 0, 0);
                    Notify.show();
    
                    Handler cancelToast = new Handler();
                    cancelToast.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Notify.cancel();
                        }
                    }, 1000);
    
                }
            });
        }
    }
    

4 个答案:

答案 0 :(得分:0)

全局声明你的字符串

而非此行:

String displayingText = intent.getStringExtra("PassToService");

用作:

public String displayText;
displayingText = intent.getStringExtra("PassToService");

答案 1 :(得分:0)

您的问题是您没有在TimeDisplayTimerTask课程中声明,您应该使用displayingText作为全局变量。

尝试使用

String displayingText;

private class TimeDisplayTimerTask extends TimerTask {
    @Override
    public void run() {

        // run on another thread
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                // display toast at every 10 second
                final Toast Notify = Toast.makeText(getApplicationContext(), displayingText, Toast.LENGTH_SHORT);
                Notify.setGravity(Gravity.CENTER, 0, 0);
                Notify.show();

                Handler cancelToast = new Handler();
                cancelToast.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Notify.cancel();
                    }
                }, 1000);

            }
        });
    }
}

并在代码中获得价值。

public int onStartCommand (Intent intent, int flags, int startId) {
    displayingText = intent.getStringExtra("PassToService");
    Toast.makeText(this, displayingText, Toast.LENGTH_SHORT).show();
    // edited here ,change the return value 
    return super.onStartCommand(intent, flags, startId);
}

修改

getStringExtra

之后使用计时器
private String displayingText;

public int onStartCommand (Intent intent, int flags, int startId) {
    displayingText = intent.getStringExtra("PassToService");
    //SharedPreferences prefs = getSharedPreferences("MyPrefs",MODE_PRIVATE);
    //String displayingText = prefs.getString("data","");
    Toast.makeText(this, displayingText, Toast.LENGTH_SHORT).show();
    // cancel if service is  already existed

    mTimer=new Timer(); // recreate new timer
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0,INTERVAL);// schedule task
    //Intent intent = getIntent();
    //String displayingText = intent.getStringExtra("PassToService");

    return START_STICKY;
}


//inner class of TimeDisplayTimerTask
private class TimeDisplayTimerTask extends TimerTask {
    @Override
    public void run() {

        // run on another thread
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                // display toast at every 10 second
                final Toast Notify = Toast.makeText(getApplicationContext(), displayingText, Toast.LENGTH_SHORT);
                Notify.setGravity(Gravity.CENTER, 0, 0);
                Notify.show();

                Handler cancelToast = new Handler();
                cancelToast.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Notify.cancel();
                    }
                }, 1000);
            }
        });
    }
}

修改

你可以测试我的代码。并为你改变一些东西。

public class MyService extends Service {

public static final String TAG = "MyService";
private Timer timer = new Timer();
private static final int UPDATE = 1;

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case UPDATE:
                Log.d(TAG, "onStartCommand() executed");
                break;
        }
    }
};

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            Message message = new Message();
            message.what = UPDATE;
            mHandler.sendMessage(message);
        }
    }, 1000, 5000);
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy() executed");
    timer.cancel();
    timer = null;
}

答案 2 :(得分:0)

试试这个:

String displayText;

public int onStartCommand (Intent intent, int flags, int startId) {
displayingText = intent.getStringExtra("PassToService");
Toast.makeText(this, displayingText, Toast.LENGTH_SHORT).show();
return START_STICKY;
}

private class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {

    // run on another thread
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            // display toast at every 10 second
            final Toast Notify = Toast.makeText(getApplicationContext(), displayingText, Toast.LENGTH_SHORT);
            Notify.setGravity(Gravity.CENTER, 0, 0);
            Notify.show();

            Handler cancelToast = new Handler();
            cancelToast.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Notify.cancel();
                }
            }, 1000);

        }
    });
}
}

答案 3 :(得分:0)

谢谢大家的帮助。我真的很感激他们。 我找到了解决传球的方法。 而不是试图按照KeLiuyue为我做的那样继续返回价值。 (长时间的讨论) 我刚刚调整了一下。

  1. 包含一个新变量。
  2.   

    private String serviceText;

    1. 定义新变量的服务价值。
    2. public int onStartCommand (Intent intent, int flags, int startId) {
          String displayingText = intent.getStringExtra("PassToService");
          Toast.makeText(this, displayingText, Toast.LENGTH_SHORT).show();
          serviceText = displayingText;
      

      如果有任何建议,请告诉我。