我想保存用户活动的详细信息,即使他通过按后退按钮或更改方向关闭了应用程序。我使用了共享预防但我不知道它是如何工作的。我在不同的程序中看到了使用sharepreferences保存数据。但我不能这样做。我必须做任何改变。 这是我的代码:
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 :(得分: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();