Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在我的服务类中修改MainActivity中的变量,然后使用变量更新TextView_Java_Android - Fatal编程技术网

Java 在我的服务类中修改MainActivity中的变量,然后使用变量更新TextView

Java 在我的服务类中修改MainActivity中的变量,然后使用变量更新TextView,java,android,Java,Android,每当运行服务时,我都试图修改MainActivity.class SharedPrefs中的一个变量(Happiness)。我似乎不知道该怎么做。请在下面找到我的代码,我的结果是happiness=-1,这是因为prefEditor.putInt我理解,但不确定如何继续修改和返回值 public class MainActivity extends Activity { int Health = 100; private static final int Happiness = 100; in

每当运行服务时,我都试图修改MainActivity.class SharedPrefs中的一个变量(Happiness)。我似乎不知道该怎么做。请在下面找到我的代码,我的结果是happiness=-1,这是因为prefEditor.putInt我理解,但不确定如何继续修改和返回值

public class MainActivity extends Activity {

int Health = 100;
private static final int Happiness = 100;
int Hunger = 0;
int Level = 1;
EditText happiness_display,
         health_display,
         hunger_display,
         level_display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        SharedPreferences settings = getSharedPreferences("APP_PREFS",
                                                  MODE_PRIVATE);
        SharedPreferences.Editor prefEditor = settings.edit();
        prefEditor.putInt("HAPPINESS", Happiness);
        prefEditor.commit();

        WebView myWebView = (WebView) findViewById(R.id.pet_display);
        myWebView.loadUrl("file:///android_asset/renamon.gif");
        myWebView.setInitialScale(10000);


        ShowToast();

        TextView happiness = (TextView) findViewById(R.id.happiness_display);
        happiness.setText(Integer.toString(Happiness));

        Calendar cal = Calendar.getInstance();

        Intent intent = new Intent(this, chronos.class);
        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

        AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

        // Start every 30 seconds
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent); 
    }


    private void ShowToast() {
        Toast.makeText(getApplicationContext(), "Toast method invoked from on create " + Happiness, Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

public class chronos extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        SharedPreferences settings = getSharedPreferences("APP_PREFS",
                MODE_PRIVATE);
        SharedPreferences.Editor prefEditor = settings.edit();
        prefEditor.putInt("HAPPINESS",  - 1);
        prefEditor.commit();
        Toast.makeText(getBaseContext(), "Chronos running" + settings.getInt("HAPPINESS", 0), Toast.LENGTH_LONG).show();


        return super.onStartCommand(intent, flags, startId);
    }
}
如果我的方法是错误的,任何帮助或正确方向上的一点都是值得赞赏的(可能是:p)

这可能是我的语无伦次,但我基本上想修改幸福、健康等的价值观。。每次运行该服务时,就像一个游戏循环,但针对虚拟宠物

my IntentService中的示例代码

happiness = happiness - 1;
        SharedPreferences.Editor prefEditor = settings.edit();
        prefEditor.putInt("HAPPINESS", happiness);
        prefEditor.commit();
我只是希望每次服务运行时,它都能从一些变量中减去一个设定值。

试试这个

首先,使用
IntentService
而不是
Service
,因为它会阻塞UI线程

像这样

public class chronos extends IntentService {

    int happiness;
    public chronos() {
        super("chronos");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub

        SharedPreferences settings = getSharedPreferences("APP_PREFS",
                MODE_PRIVATE);

            happiness = settings.getInt("HAPPINESS",0) - 1;

            SharedPreferences.Editor prefEditor = settings.edit();
            prefEditor.putInt("HAPPINESS",happiness);
            prefEditor.commit();

        Toast.makeText(getBaseContext(),
                "Chronos running" + settings.getInt("HAPPINESS", 0),
                Toast.LENGTH_LONG).show();

    }

}

如果要从服务更新MainActivity的
TextView
值,需要使用不会修改存储在SharedRef键“Happiness”中的Happiness变量值的

?我要澄清的toast消息是为了调试,例如它显示的值是“-1”,而不是新的幸福值应该是“99”(100-1),它不能执行算术运算,您需要单独执行此操作,然后将结果存储回共享首选项。请您解释一下如何执行实际的算术运算好吗?每次服务运行时,它都应该从主活动更新您的视图??你能告诉我你在服务中要做什么样的操作吗?主要是从主活动中设置的变量中减去,例如每半小时减去一次快乐,减去三次饥饿等等。。