Monday, March 3, 2014

addroid multithreading problem help


Runnable mUpdateResults = new Runnable () {
public void run () {
}
};
For the above thread, every time you start a thread to get the value of the coins SharedPreferences field in the run method, why is the initial value of the coins to get the.


SharedPreferences code
package com.example.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SaveCoinsUtil {
private String netCoins = "0";
private String coins = "0";
private SharedPreferences sp;
private SharedPreferences.Editor editor;
public SaveCoinsUtil (Context context) {
sp = context.getSharedPreferences ("guesstitle", Context.MODE_PRIVATE);
coins = sp.getString ("coins", "0");
netCoins = sp.getString ("netCoins", "0");
editor = sp.edit ();
}

public String getCoins () {
return coins;
}
public void setCoins (String coins) {
this.coins = coins;
. sp.edit () putString ("coins", coins) commit ();.
}

public String getNetCoins () {
return netCoins;
}
public void setnetCoins (String netCoins) {
this.netCoins = netCoins;
. sp.edit () putString ("netcoins", coins) commit ();.
}
}
java code
package com.example.util;
import android.content.Context;
import android.content.SharedPreferences;
public class SaveCoinsUtil {
private String netCoins = "0";
private String coins = "0";
private SharedPreferences sp;
private SharedPreferences.Editor editor;
public SaveCoinsUtil (Context context) {
sp = context.getSharedPreferences ("guesstitle", Context.MODE_PRIVATE);
coins = sp.getString ("coins", "0");
netCoins = sp.getString ("netCoins", "0");
editor = sp.edit ();
}

public String getCoins () {
return coins;
}
public void setCoins (String coins) {
this.coins = coins;
. sp.edit () putString ("coins", coins) commit ();.
}

public String getNetCoins () {
return netCoins;
}
public void setnetCoins (String netCoins) {
this.netCoins = netCoins;
. sp.edit () putString ("netcoins", coins) commit ();.
}
}
java code
package com.example.testappgetcions;

import com.example.util.SaveCoinsUtil;
import cn.waps.AppConnect;
import cn.waps.UpdatePointsNotifier;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements UpdatePointsNotifier {
private Button btn;
private TextView tView;
private String displayPointsText;
private String currencyName = "gold";
int netCoins;
String saveNetcoin;
final Handler mHandler = new Handler ();
SaveCoinsUtil scUtil;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
addNetCoins ();
AppConnect.getInstance ("09f277ca386ee99cb4c910e09f562112", "360", this);
tView = (TextView) findViewById (R.id.textView1);
btn = (Button) findViewById (R.id.button1);
scUtil = new SaveCoinsUtil (this);
btn.setOnClickListener (new OnClickListener () {

@ Override
public void onClick (View v) {
. AppConnect.getInstance (MainActivity.this) showOffers (MainActivity.this);
}
});
}



Runnable mUpdateResults = new Runnable () {


public void run () {

Log.i ("1", "--->" + scUtil.getNetCoins ());

if (netCoins> Integer.parseInt (saveNetcoin)) {
scUtil.setnetCoins (netCoins + "");
Log.i ("2", "--->" + scUtil.getNetCoins ());
scUtil.setCoins (100 + Integer.parseInt (scUtil.getCoins ()) + "");
tView.setText (scUtil.getCoins ());
tView.setTextColor (Color.BLACK);
}
}
};


@ Override
public void getUpdatePoints (String currencyName, int pointTotal) {
this.currencyName = currencyName;
displayPointsText = currencyName + ":" + pointTotal;
netCoins = pointTotal;
mHandler.post (mUpdateResults);
}
@ Override
public void getUpdatePointsFailed (String error) {
displayPointsText = error;
mHandler.post (mUpdateResults);
}

@ Override
protected void onResume () {
. AppConnect.getInstance (this) getPoints (this);
super.onResume ();
}

@ Override
protected void onDestroy () {
. AppConnect.getInstance (this) close ();
super.onDestroy ();
}

private void addNetCoins () {
if (scUtil.getNetCoins (). isEmpty ()) {
scUtil.setnetCoins ("0");
}
saveNetcoin = scUtil.getNetCoins ();
}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
After setting the coins, and commit the future, then immediately see the value of coins taken out right.
Reply:
Use the simulator to see the value is not already written into it, is not without the commit operation
Reply:
Is not written not go
Reply:
Upstairs three did not know did not see the LZ code stickers
Reply:
This.coins ghosts are engaged, it has presence necessary?
Reply:
Key for a mistake! ! !

No comments:

Post a Comment