Search This Blog

Showing posts with label SharedPreferences. Show all posts
Showing posts with label SharedPreferences. Show all posts

Thursday, May 9, 2013

Get the number of times app is used




 private SharedPreferences mPref;
public static final String PREFS_NAME = "YourPrefName";
int c;

in onCreate() write the following



  mPref = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
c = mPref.getInt("numRun", 0);
Log.i("TAG", "Number of count of open App:" + c);
c++;
mPref.edit().putInt("numRun", c).commit();
Log.i("TAG", "Number of count after commit of open App:" + c);

Saturday, December 17, 2011

Give preferences in the screen

First of all create the preference.xml file and enter in it.


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/xml/flightoptions.xml -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="using_categories_in_root_screen"
android:title="Categories"
android:summary="Using Preference Categories">
<PreferenceCategory
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="meats_screen"
android:title="Meats"
android:summary="Preferences related to Meats">
<CheckBoxPreference
android:key="fish_selection_pref"
android:title="Fish"
android:summary="Fish is great for the healthy" />
<CheckBoxPreference
android:key="chicken_selection_pref"
android:title="Chicken"
android:summary="A common type of poultry" />
<CheckBoxPreference
android:key="lamb_selection_pref"
android:title="Lamb"
android:summary="Lamb is a young sheep" />
</PreferenceCategory>
<PreferenceCategory
    xmlns:android="http://schemas.android.com/apk/res/android"
android:key="meats_screen"
android:title="Foods"
android:summary="Preferences related to Meats">
<CheckBoxPreference
android:key="tomato_selection_pref"
android:title="Tomato "
android:summary="It's actually a fruit" />
<CheckBoxPreference
android:key="potato_selection_pref"
android:title="Potato"
android:summary="My favorite vegetable" />
</PreferenceCategory>
</PreferenceScreen>


And make a java file preferencescreens.java of the following.


package org.apache.android;


import android.os.Bundle;
import android.preference.PreferenceActivity;


public class preferencescreens extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
}
}

Now make an main.xml file



<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:text="" android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


make a main.java file


package org.apache.android;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;


public class mainActivity extends Activity {



private TextView tv = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.text1);
setOptionText();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}


@Override
public boolean onOptionsItemSelected (MenuItem item)
{
if (item.getItemId() == R.id.menu_prefs)
{
Intent intent = new Intent().setClass(this, preferencescreens.class);
this.startActivityForResult(intent, 0);
}
else if (item.getItemId() == R.id.menu_quit)
{
finish();
}
return true;
}


@Override
public void onActivityResult(int reqCode, int resCode, Intent data)
{
super.onActivityResult(reqCode, resCode, data);
setOptionText();
}




private void setOptionText()
{
SharedPreferences prefs = getSharedPreferences("Sharedpreference", 0);
String option = prefs.getString(this.getResources().getString(R.string.selected_flight_sort_option),
this.getResources().getString(R.string.flight_sort_option_default_value));
String[] optionText = this.getResources().getStringArray(R.array.flight_sort_options);
tv.setText("option value is " + option + " (" + optionText[Integer.parseInt(option)] + ")");
}
}