SharedPreference in android(Tell friend + Disable Dialog notification) - java

i am developing an android application that has a shared preference, containing :
- Ringtone (done)
disable the dialog notification (alarm, SMS, MMS)(not yet,please read the comment inside the code)
-Tell friend (i am sure my code is right but i am not sure how to put it with Shared preference + i am now sure of place and method that i should put at it- please read to the comment inside the code)
public class Setting extends PreferenceActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
String ringtonePreference;
// Get the xml/preferences.xml preferences
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
ringtonePreference = prefs.getString("ringtonePref",
"DEFAULT_RINGTONE_URI");
}
//disable all Notification (Alarm , SMS,MMS)
//what is the method that i should put this code at it? onStart?onDestroy?or what?
SharedPreferences sharedPreferences = this.getPreferences(MODE_PRIVATE);
boolean hideNotification = sharedPreferences.getBoolean("checkbox", false);
if(!hideNotification)
alertDialog.show();
}
//tell friend
//i am not sure if this is right place to this code
//if it is the right place then what is the method that i should put this code at it?onStart?onActivityResult?or what?
SharedPreferences prefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
TellFriendPreference = prefs1.getString("tellfriend","null");
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
String str= item.toString();
if(str=="Tell Friends"){
Toast.makeText(this,"First", Toast.LENGTH_LONG).show();
//Go to tell friend activity
//Intent i = new Intent (Help.this, .class);
//startActivity(i);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Check out s Application for Android. Download it today from http://s.com");
startActivity(Intent.createChooser(intent, "Tell friends:"));
startActivity(intent);
}
this is prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Second Category" >
<RingtonePreference
android:name="Ringtone Preference"
android:key="ringtonePref"
android:summary="Select a ringtone"
android:title="Ringtones" />
<CheckBoxPreference
android:defaultValue="true"
android:key="checkbox"
android:summary="Check the Box"
android:title="Disable Notification" />
<Preference
android:name="tell Preference"
android:key="tellfriend"
android:summary="if u want to tell ur friends by usinf"
android:title="Tell Friend" />
</PreferenceCategory>
</PreferenceScreen>
please help me, i should submit it after some hours

I solved my problem. I just but tell friend in another activity, my code is correct.

Related

How to check if a given package is disabled or not and then show toast message accordingly?

I'm making an app where it uses intent to send data to another app. In case, the other app, which is supposed to receive data from my app, is not installed on users's device then it redirects user to play store with toast message asking user to install it. I used "if else" to achieve this. It worked all good until I found that if the other app is disabled by user (OEM installed app which can't be uninstalled), then my app crashes. In such a condition, I want to let user know that the app is disabled by them and ask them to enable it (through toast message). How can I achieve this?
Here is my complete code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates button view which is connected to a view in the XML layout, which gets triggered on touching the view.
TextView textView = (TextView) findViewById(R.id.location);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Use package name which we want to check
boolean isAppInstalled = appInstalledOrNot("com.google.android.apps.maps");
if(isAppInstalled){
Uri gmmIntentUri = Uri.parse("geo:00,0000,00,0000");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else {
Uri uri2 = Uri.parse("market://details?id=com.google.android.apps.maps");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri2);
Toast.makeText(MainActivity.this, "Google Maps not Installed", Toast.LENGTH_SHORT).show();
startActivity(goToMarket);
}
}
});
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
}
I'm not sure if this will work for you, but since you know the package name, you could try this to do a check beforehand.

Android: SharedPreferences not getting updated from PreferenceFragment

I'm posting this question again as I didn't get any answers last time, and I still haven't solved the problem.
I have a settings menu with a PreferenceScreen in which I create a lot of CheckBoxPreferences during runtime (Adding them into the "Exclusion List" prefscreen). I created them no problem, here's the XML code below that it starts with:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:key="exclusion_list_key"
android:title="Exclusion List"
android:persistent="true"
android:summary="" >
</PreferenceScreen>
</PreferenceScreen>
I create the checkboxes in the onCreate method of my PreferenceFragment and add them to the "Exclusion List" PreferenceScreen and that works fine.
I'm trying to set the summary of the "Exclusion List" to be a list of all the checkbox titles that are checked off (so if the checkbox is checked, it's name will be listed in the summary of the "Exclusion List").
In the onCreate() method, the summary gets set properly, there's no problem.
But in the onPreferenceChanged() method, I set the summary of "Exclusion List" to the 'summary' string I built (which contains the correct value), but it doesn't update it! When i press back from my checkbox menu, the "Exclusion List" does not have the updated values.
The last few lines are the ones of interest. I did some printlns to see what's going on:
The listener works fine, runs when expected
My summary var contains what's expected
After calling setSummary(summary), the getSummary() returns the expected value (so that means it got set properly)
However, when I actually press back and see "Exclusion List", it's summary doesn't actually get updated!
Did I miss something? Thanks in advance!
All the code for reference:
public class Settings extends AppCompatActivity {
public static final String EXC_LIST_KEY = "exclusion_list_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
String summary = "";
//Create all the checkboxes inside of the PreferenceScreen
for (int i = 0; i < MainActivity.contactNames.length; i++) {
CheckBoxPreference checkbox = new CheckBoxPreference(getActivity());
checkbox.setTitle(MainActivity.contactNames[i][0]);
checkbox.setKey(MainActivity.contactNames[i][2]);
checkbox.setSummary(MainActivity.contactNames[i][1]);
checkbox.setDefaultValue(false);
((PreferenceScreen) findPreference(EXC_LIST_KEY)).addPreference(checkbox);
if (checkbox.isChecked()) {
summary = summary + checkbox.getTitle() + "\n";
}
}
findPreference(EXC_LIST_KEY).setSummary(summary);
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
//Checkbox keys all start with 'key_'
if (key.startsWith("key_")) {
String summary = "";
for (int i = 0; i < MainActivity.contactNames.length; i++) {
CheckBoxPreference checkbox = (CheckBoxPreference) findPreference(MainActivity.contactNames[i][2]);
if (checkbox.isChecked()) {
summary = summary + checkbox.getTitle() + "\n";
}
}
System.out.println("Summary I have: " + summary); //Correct summary is printed out
findPreference(EXC_LIST_KEY).setSummary(summary); //Isn't updating the value???
System.out.println("Summary system has: " + findPreference(EXC_LIST_KEY).getSummary()); //Correct summary is printed out
}
}
}
}
try Adding this code in your SettingsFragment
#Override
public void onResume() {
super.onResume();
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
// CHANGE 1: load saved values to set the summaries
onSharedPreferenceChanged(prefs, "exclusion_list_key");
// CHANGE 2: register shared prefs listener in onResume
prefs.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
Got it working by adding the following line of code to the end of my onSharedPreferenceChanged() method:
((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();

Android open URL onclick certain button

How i can send people to my google play on click certain button ?
i already did this in activity_menu.xml
<Button
android:id="#+id/ratebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/morebutton"
android:layout_alignBottom="#+id/morebutton"
android:layout_toLeftOf="#+id/morebutton"
android:layout_toRightOf="#+id/aboutButton"
android:background="#drawable/more"
android:text="#string/rateBtn"
android:textColor="#color/White"
android:textColorHint="#color/White"
android:textSize="18sp" />
but in MenuActivity.java i couldn't know what should i do ?
Any help? Thanks
findViewById(R.id.ratebutton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "market://details?id=<package_name>";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
Button rate = (Button)findViewById(R.id.rate);
rate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i2=new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=yourpackagename"));
startActivity(i2);
}
});
// Enter package name of application in place of yourpackagename
In my signin.xml file I put this code:
<Button
android:id="#+id/privacypolicylink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="50dp"
android:background="#android:color/transparent"
android:onClick="performSingUpMethod"
android:text="PolĂ­tica de Privacidad"
android:textColor="#A9A9A9"
android:textSize="15dp"
android:textStyle="bold" />
In my signin.java file I used this:
findViewById(R.id.privacypolicylink).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "https://example.net/privacy_policy";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
It works for me. It displays a text in the grey color that I want it. The text is underlined and it is a link. The is the way it looks:
When I click on it, I see this:
Then I can open the link using a web browser on the phone and everything works correctly the way I needed it.
Declare button inside your activity:
Button btnRateButton;
Initialize btnRateButton inside onCreate method:
btnRateButton = (Button) findViewById(R.id.ratebutton);
Set on click listener:
btnRateButton.setOnClickListener(this);
Write onclick method:
#Override
public void onClick(View v) {
Intent i = null;
final String myPackage = getPackageName();
if(v.getId() == R.id.btnRateButton) {
i = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=" + myPackage));
startActivity(i);
}
}
Note that for above code to work, you need to have Play Store app installed on your device.

Not receiving value from CheckBoxPreference in PreferenceScreen?

I'm using Eclipse's default "Settings Screen" Activity, which created a class extending PreferenceScreen. The methods being used to grab values seem strange to me, but I'm working with them. I can get the value of an EditTextPreference this way:
}else if(preference instanceof EditTextPreference) {
preference = (EditTextPreference) preference;
int p_value = Integer.valueOf(((EditTextPreference) preference).getText());
MainActivity.decimals = p_value;
This works fine for the EditTextPreference and ListPreference. However, upon trying it with a CheckBoxPreference, like so:
}else if(preference instanceof CheckBoxPreference) {
preference = (CheckBoxPreference) preference;
boolean toastEn = ((CheckBoxPreference) preference).isChecked();
if(toastEn) {
MainActivity.toastsEnabled = true;
}else MainActivity.toastsEnabled = false;
}
It does not work. From the small amount of debugging I managed to do (Toasts and Logs seem to be very limited in this method), it looks like it's just not getting the value of the CheckBoxPreference.
Am I going about this correctly?
Edit: the encapsulating method looks like this (I forgot to add it at first, sorry, it might be important):
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
You can try add a listener to the checkbox such as setOnPreferenceChangeListener or setOnPreferenceClickListener
That is really strange Preferences code.
What I did for Preferences was the following:
public class SettingsActivity extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Where the res\xml\preferences.xml looked like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:defaultValue="true"
android:key="MusicEnabled"
android:title="#string/musicenabled"
/>
<CheckBoxPreference
android:defaultValue="true"
android:key="SoundEnabled"
android:title="#string/soundenabled"
/>
<CheckBoxPreference
android:defaultValue="true"
android:key="IntroEnabled"
android:title="#string/introsoundenabled"
/>
</PreferenceScreen>
And you get its values with the following code:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
soundEnabled = sp.getBoolean("SoundEnabled", false);

Phone call click for textview

Okay so I've looked and tried to make sense of some other code that I've found but nothing has been working out for me. I'm trying to get the user to click the textview and have it take them to their phones dial-er.
Here's the Java:
public class Info extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.moreinfo);
Button appoint =(Button) findViewById(R.id.btnAppoint);
TextView phone =(TextView) findViewById(R.id.phoneTxt);
String url = phone.getText().toString();
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
appoint.setOnClickListener(new OnClickListener () {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Info.this, EmailContact.class));
}
});
phone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+"+phone.getText().toString().trim()));
startActivity(callIntent );
}
});
}
}
I've also put android: clickable = true so I don't see if that's the problem. Any help would be appreciated!
Change phone.getText().toString().trim() to use the View object supplied to the onclick method:
phone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:+"+((TextView)arg0).getText().toString().trim()));
startActivity(callIntent );
}
});
Additionally, if you just want to show the dialer with a phone number loaded, you're using the wrong intent action, Intent.ACTION_DIAL instead of Intent.ACTION_CALL will show the dialer. The Intent.ACTION_CALL that you're using will actually initiate the phone call, and to make that work, you need to add the appropriate permission to your Manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
Based on your description its red because if you're going to use an class method level object within an anonymous obejct, the object must be defined as final.
final TextView phone =(TextView) findViewById(R.id.phoneTxt);
And for further measure, ensure you are using the right permission to do the call action in your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />

Categories