2014-07-24 24 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