Android SharedPreference - Please help me improve my method : I want to run another application whose package name is obtained from EditText in AlertDialog. My problem is that I want to display Dialog only when PackageName has not been specified or when the application is not available. Here is my code:

    public static final String mypref="mypref";
	public static final String packagename="text";
	private SharedPreferences pref;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		gpref();
		Button b=(Button)findViewById(R.id.btn);
		b.setOnClickListener(new View.OnClickListener(){

					@Override
					public void onClick(View p1)
						{
							// TODO: Implement this method
							String s=pref.getString(packagename);
							if(s!=""){
								Intent i=getPackageManager().getLaunchIntentForPackage(s);
								if(i!=null){
									startActivity(i);
								} else{ sd();}
							} else{
								sd();
							}
						}
				});
    }
	public void sd(){
		final EditText et=new EditText(this);
		AlertDialog ad=new AlertDialog.Builder(MainActivity.this).create();
		ad.setView(et); ad.setButton(AlertDialog.BUTTON_POSITIVE,
				"Set", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface p1, int p2)
							{
								// TODO: Implement this method
								String s=et.getText().toString();
								spref(s);
							}
					});
	}
	public void gpref(){
		pref=getSharedPreferences(mypref,MODE_PRIVATE);
	}
	public void spref(String s){
		gpref();
		SharedPreferences.Editor spedit=pref.edit();
		spedit.putString(packagename,s); spedit.apply();
	}
