I have a main activity where I call
VersionSettings vs = new VersionSettings(this);
if (vs.firstRun2())
vs.versionCheckbox();
What I'm trying to do is set a checkbox (checkboxVideoType) to an unchecked state if the Android version is 1.6-2.1
I only want to do this the very first time the app is ever run, it never needs to run again.
I think I'm running into the problem when the main activity calls versionCheckbox(), I get a force close if it attempts to run the code inside that is going to set the checkbox to false.
I'm very new to programming and would really appreciate some help with this. I think I'm close, but need a push. Thanks in advance!
Main Activity
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
public class Nasatv extends Activity implements OnClickListener {
boolean checkboxIsChecked;
SharedPreferences nasaTV_Prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
nasaTV_Prefs = getSharedPreferences("nasaTV_Prefs", 0);
ChangeLog cl = new ChangeLog(this);
if (cl.firstRun())
cl.getLogDialog().show();
VersionSettings vs = new VersionSettings(this);
if (vs.firstRun2())
vs.versionCheckbox();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Nasatv.this, LaunchCalendar.class);
startActivity(i);
}
});
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Nasatv.this, PhotoInfo.class);
startActivity(i);
}
});
CheckConnectivity check = new CheckConnectivity();
Boolean conn = check.checkNow(this.getApplicationContext());
if(conn == true){
ImageView updateImage = (ImageView) findViewById(R.id.updateImage);
ImageDownloader downloader = new ImageDownloader(updateImage);
downloader.execute("http://www.url.com/trl/ubox.jpg");
} else {
ImageView updateImage = (ImageView) findViewById(R.id.updateImage);
updateImage.setImageResource(R.drawable.uboxerror);
}
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.setting_title:
Intent settingsActivity =new Intent(getBaseContext(), Settings.class);
startActivity(settingsActivity);
return true;
case R.id.photo_archive:
Intent archive = new Intent(Nasatv.this, PhotoArchive.class);
startActivity(archive);
return true;
case R.id.n_web:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.nasa.gov/"));
startActivity(intent);
return true;
case R.id.exit_title:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onResume() {
super.onResume();
checkboxIsChecked = nasaTV_Prefs.getBoolean("checkboxVideoType", true);
}
@Override
public void onClick(View v) {
if (checkboxIsChecked) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.nasa.gov/multimedia/nasatv/nasatv_android_flash.html"));
startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("rtsp://nasadln.qt.llnwd.net/nasa101.sdp"));
startActivity(intent);
}
}
}
One-time run class
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.CheckBox;
public class VersionSettings {
private final Context context;
private String notRun, hasRun;
private SharedPreferences run;
private CheckBox checkboxVideoType;
private SharedPreferences nasaTV_Prefs;
private static final String HAS_RUN = "PREFS_HAS_RUN";
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
/**
* Constructor
*
* Retrieves whether the app has been run or not and saves to
* SharedPreferences
*/
public VersionSettings(Context context) {
this.context = context;
this.run = PreferenceManager.getDefaultSharedPreferences(context);
// get run/not run string number, which is "1"
this.notRun = run.getString(HAS_RUN, "");
Log.d(TAG, "notRun: " + notRun);
this.hasRun = context.getResources().getString(R.string.has_run_string);
Log.d(TAG, "hasRun: " + hasRun);
// save new number to preferences, which will be the same number,
// so this is run only the very first time the app is run
SharedPreferences.Editor editor = run.edit();
editor.putString(HAS_RUN, hasRun);
editor.commit();
}
/**
* @return true if this version of your app is started for the first
* time
*/
public boolean firstRun2() {
return ! notRun.equals(hasRun);
}
/**
* @return Change the checkboxVideoType to "unchecked" (false)
*
*/
public void versionCheckbox() {
// this.context = context;
if (currentapiVersion < android.os.Build.VERSION_CODES.FROYO){
this.nasaTV_Prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = nasaTV_Prefs.edit();
editor.putBoolean("checkboxVideoType", false);
editor.commit();
}
}
private static final String TAG = "VersionSettings";
}
Preferences Activity
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class Settings extends Activity implements OnCheckedChangeListener {
private CheckBox checkboxVideoType;
private SharedPreferences nasaTV_Prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.preferences);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
checkboxVideoType = (CheckBox) findViewById(R.id.checkboxVideoType);
checkboxVideoType.setOnCheckedChangeListener(this);
nasaTV_Prefs = getSharedPreferences("nasaTV_Prefs", 0);
checkboxVideoType.setChecked(nasaTV_Prefs.getBoolean("checkboxVideoType", true));
Button clbutton = (Button) findViewById(R.id.clbutton);
clbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ChangeLog cl = new ChangeLog(Settings.this);
cl.getFullLogDialog().show();
}
});
}
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
if (cb == checkboxVideoType){
SharedPreferences.Editor editor = nasaTV_Prefs.edit();
editor.putBoolean("checkboxVideoType", isChecked);
editor.commit(); // Commit the edit, i.e., save the state of the flag!
}
}
}