2016-08-15 63 views
0

更新:如果其他地方出現問題,我添加了整個MainService和MainActivity。此外,我還添加了下面提供的解決方案,但它仍不會更新TextView。SharedPreferences在嘗試更新TextView時不起作用

我想改變一個TextView,我希望它是永久的,所以我使用SharedPreferences。可悲的是,有些事情是錯誤的,因爲TextView沒有更新。

MainService:

public class MainService extends Service { 

public static final String BROADCAST_ACTION = "com.example.vladpintea.friendsbeforecents.displayevent"; 
private final Handler handler = new Handler(); 
int counterr = 0; 
Intent intentt; 

String usedTimer; 
long interval; 

//TimerTask that will cause the run() runnable to happen. 
TimerTask myTask = new TimerTask() { 
    public void run() { 
     stopSelf(); 
    } 
}; 
//Timer that will make the runnable run. 
Timer myTimer = new Timer(); 

@Override 
public void onCreate() { 
    intentt = new Intent(BROADCAST_ACTION); 

    registerReceiver(counter, new IntentFilter(Intent.ACTION_SCREEN_ON)); 

    Toast.makeText(MainService.this, "Service, Created", Toast.LENGTH_SHORT).show(); 
} 

private BroadcastReceiver counter = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(MainService.this, "Whoops! You've Lost.", Toast.LENGTH_SHORT).show(); 
     Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000); 
    } 
}; 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Toast.makeText(MainService.this, "Service, Started", Toast.LENGTH_SHORT).show(); 

    usedTimer = intent.getStringExtra("timer"); 
    try { 
     interval = Long.parseLong(usedTimer); 
    } catch (NumberFormatException ignored) {} 

    myTimer.schedule(myTask, interval); 

    handler.removeCallbacks(sendUpdatesToUI); 
    handler.postDelayed(sendUpdatesToUI, 1000); 

    return super.onStartCommand(intent, flags, startId); 
} 

private Runnable sendUpdatesToUI = new Runnable() { 
    public void run() { 
     handler.postDelayed(this, 1000); 
    } 
}; 

public void DisplayLoggingInfoPlus() { 
    intentt.putExtra("counter", String.valueOf(++counterr)); 
    sendBroadcast(intentt); 
} 

@Override 
public void onDestroy() { 
    DisplayLoggingInfoPlus(); 

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK 
      | PowerManager.ACQUIRE_CAUSES_WAKEUP 
      | PowerManager.ON_AFTER_RELEASE, "MyWakeLock"); 
    wakeLock.acquire(); 

    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000); 
} 

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

MainActivity:

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final Button startApp = (Button) findViewById(R.id.startApp); 
    final EditText timer = (EditText) findViewById(R.id.insertTimer); 

    assert startApp != null; 
    startApp.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(MainActivity.this, "Countdown, Started", Toast.LENGTH_SHORT).show(); 

      Intent intent = new Intent(MainActivity.this, MainService.class); 

      assert timer != null; 
      intent.putExtra("timer", timer.getText().toString()); 

      Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000); 

      registerReceiver(broadcastReceiver, new IntentFilter(MainService.BROADCAST_ACTION)); 

      startService(intent); 
     } 
    }); 
} 

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     updateUI(intent); 
    } 
}; 

private void updateUI(Intent intent) { 
    String counter = intent.getStringExtra("counter"); 
    TextView txtCounter = (TextView) findViewById(R.id.txtCounter); 
    assert txtCounter != null; 

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString(counter,txtCounter.getText().toString()); 
    editor.commit(); 
} 

}

+0

的可能的複製[如何使用SharedPreferences在Android的存儲,讀取和編輯值(http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – piotrek1543

+0

asnswer在上面的鏈接中。您保存值,但不檢索 – piotrek1543

回答

0

可悲的是,什麼是錯的,因爲TextView的不更新。

您需要通過調用的setText()來更新的TextView:

String counter = intent.getStringExtra("counter"); 
TextView txtCounter = (TextView) findViewById(R.id.txtCounter); 
txtCounter.setText(counter); 
0

更改以下行

editor.putString(String.valueOf(txtCounter), counter); 

editor.putString("counter",txtCounter.getText().toString()); 
+0

在txtCounter.getText()我得到一個錯誤,說這是一個錯誤的參數類型。 –

+0

增加了一個toString() – gaara87

+0

仍然沒有運氣。沒有任何錯誤,它只是不會更新。 –

0

存儲值在共享偏好:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
SharedPreferences.Editor editor = preferences.edit(); 
editor.putString("Counter",String.valueOf(txtCounter)); 
editor.apply(); 

要從共享偏好檢索值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
String Counter= preferences.getString("Counter", ""); 
if(!Counter.equalsIgnoreCase("")) 
{ 
    Counter= Counter+ " Counter"; /* Edit the value here*/ 
}