How To Use Sharedpreferences From Alertbox?
Hi i created one media player. when my media player application load that time my dialog box also first display.... but now i am expecting only load once my dialog box..if i run 1s
Solution 1:
Check this Code, I edited your code using Shared Preference :
What i did is that , I kept a Flag in Shared Preference ,if the Application is launched it checks for value in Shared Preference , true -- Will not show Dialog , False -- Calls the Dialog Method and at the End of the Method it will change the Value to true , so next time it will shows Dialog.
publicclassMap_ViewextendsActivity {
//Shared Preferences Variables //static SharedPreferences settings;
publicstatic SharedPreferences.Editor editor;
publicstaticfinalStringPREFS_NAME="TEST";
//End /** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle State) {
super.onCreate(State);
_context = getApplicationContext();
setContentView(R.layout.main);
settings=getSharedPreferences(PREFS_NAME, 0);
editor = settings.edit();
if(settings.getBoolean("Alert_Dialog", false)==false){
alert_dialog();
}
//set GridView for gallery
_gallery = (Gallery) findViewById(R.id.videoGrdVw);
//set default as external/sdcard uri
_contentUri = MEDIA_EXTERNAL_CONTENT_URI;
//initialize the videos uri//showToast(_contentUri.getPath());
initVideosId();
//set gallery adapter
setGalleryAdapter();
}
privatevoidalert_dialog() {
// TODO Auto-generated method stub
AlertDialog.Builderalertbox=newAlertDialog.Builder(this);
alertbox.setMessage("Disclaimer popup window sample..");
alertbox.setPositiveButton("Agree/Close", newDialogInterface.OnClickListener() {
// do something when the button is clickedpublicvoidonClick(DialogInterface dialog, int arg1) {
dialog.cancel();
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("Buy", newDialogInterface.OnClickListener() {
// do something when the button is clickedpublicvoidonClick(DialogInterface arg0, int arg1) {
arg0.cancel();
}
});
// display box
alertbox.show();
editor.putBoolean("Alert_Dialog", true);
editor.commit();
}
}
Post a Comment for "How To Use Sharedpreferences From Alertbox?"