2014-07-24 64 views
2

即使通過按下後退按鈕或更改方向關閉了應用程序,我仍想保存用戶活動的詳細信息。我使用共享的prefences,但我不知道它是如何工作的.i在不同的程序中使用共享首選來保存數據。但我不能這樣做。是否有任何改變我必須做。 這裏是我的代碼:在活動識別應用程序中使用共享首選項

package tmt.niranjan.travellingtrack; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesClient; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.location.ActivityRecognitionClient; 

public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener { 

private ActivityRecognitionClient arclient; 
private PendingIntent pIntent; 
private BroadcastReceiver receiver; 
private TextView tvActivity; 
public static final String SHARED_PREFERENCES = 
     "tmt.niranjan.travellingtrack.SHARED_PREFERENCES"; 
public static final String KEY_LOG_FILE_NUMBER = 
      "tmt.niranjan.travellingtrack.KEY_LOG_FILE_NUMBER"; 
    public static final String KEY_LOG_FILE_NAME = 
      "tmt.niranjan.travellingtrack.KEY_LOG_FILE_NAME"; 
    public static final String KEY_LOG_FILE_NAME1 = 
      "tmt.niranjan.travellingtrack.KEY_LOG_FILE_NAME"; 
    public static final String KEY_PREVIOUS_ACTIVITY_TYPE = 
      "tmt.niranjan.travellingtrack.KEY_PREVIOUS_ACTIVITY_TYPE"; 
private SharedPreferences mpref; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tvActivity = (TextView) findViewById(R.id.Actrec); 
    mpref = getApplicationContext().getSharedPreferences(
      SHARED_PREFERENCES, Context.MODE_PRIVATE); 

    int resp =GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if(resp == ConnectionResult.SUCCESS){ 
     arclient = new ActivityRecognitionClient(this, this, this); 
     arclient.connect(); 


    } 
    else{ 
     Toast.makeText(this, "Please install Google Play Service.", Toast.LENGTH_SHORT).show(); 
    } 

    receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Editor editor = mpref.edit(); 
      String v = "Type:"+intent.getExtras().getInt("Type")+" "+"Activity :" + intent.getStringExtra("Activity") + " " + "Confidence : " + intent.getExtras().getInt("Confidence") + "\n"; 
      editor.putInt(KEY_PREVIOUS_ACTIVITY_TYPE,intent.getExtras().getInt("Type")); 
      editor.putString(KEY_LOG_FILE_NAME, "Activity"); 
      editor.putInt(KEY_LOG_FILE_NUMBER, intent.getExtras().getInt("Confidence")); 
      editor.putString(KEY_LOG_FILE_NAME1,v); 



      tvActivity.setText(v); 

      editor.commit(); 
     } 
     }; 

    IntentFilter filter = new IntentFilter(); 
    filter.addAction("tmt.niranjan.myactivityrecognition.ACTIVITY_RECOGNITION_DATA"); 
    registerReceiver(receiver, filter); 

} 


@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if(arclient!=null){ 
     arclient.removeActivityUpdates(pIntent); 
     arclient.disconnect(); 
    } 
    Toast.makeText(this, "ondestroycalled", Toast.LENGTH_SHORT).show(); 
    unregisterReceiver(receiver); 
} 

@Override 
public void onConnectionFailed(ConnectionResult arg0) { 
    Toast.makeText(this, "Connection Failed", Toast.LENGTH_SHORT).show(); 
} 
@Override 
public void onConnected(Bundle arg0) { 
    Intent intent = new Intent(this, ActivityRecognitionService.class); 
    pIntent = PendingIntent.getService(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); 
    arclient.requestActivityUpdates(1000, pIntent); 
} 
@Override 
public void onDisconnected() { 
} 

}

+0

'但是我不能這樣做it' - ????? 'onDestroy()'是一個不錯的選擇。 'onPause()'或'onStop()'可能更好。 – Simon

+0

真的不清楚你在問什麼。將這些值從SharedPreferences中取出的方式是調用'mPrefs.getInt(NAME,DEFAULT_VALUE)'並將其更改爲整數,布爾值,字符串等。我沒有看到任何您試圖獲取這些值的地方。 – anthonycr

+0

在這裏,我想通過tvActivity.setText(v)來訪問值。我將字符串v值保存在sharedpreferences中。我期待得到v的先前和現在的價值是錯誤的? – Niranjan

回答

0

你缺少editor.apply();在editor.commit()之前;

給它一個去看看它是否工作

這裏是我使用和正常工作的代碼,所以如果不是editor.apply那麼它會建議什麼都沒有從你的intents.getextra設置,你有沒有調試過,看看它們是否包含一個值?

SharedPreferences.Editor editor = prefs.edit(); 
      editor.putString("key_pref_branch_code", mainObject.getString("Code")); 
      editor.putString("key_pref_branch_id", mainObject.getString("ID")); 
      editor.putString("key_pref_branch_name", mainObject.getString("Name")); 
      editor.putString("key_pref_json_url", "http://xxxxx:8080/storehandler.ashx"); 
      editor.apply(); 
      editor.commit(); 
+0

我添加了editor.apply(),但它不起作用。 PLZ通過代碼建議我如果有我犯了什麼錯誤 – Niranjan

+1

editor.apply()和editor.commit()基本上是同義詞(http://stackoverflow.com/questions/5960678/whats-the-difference-between-commit並且應用於共享偏好),但這不是答案。 – anthonycr

+0

@ A.C.R.發展,謝謝,我不知道這 – dave