can I make ringtone in preferences - java

How can I make ringtone activity (that always appear in setting) so the user can choose her ringtone from system ringTones I googled it but I didn't find complete tutorial, I am really confused, please give me tutorial or some codes.
Also, if I want the user to choose the special ringtone to Notification in my application should i use Shared preference or preference?
I already did the Menu:
// Menu Code Part#2
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
case R.id.setting:
startActivity(new Intent(this, Setting.class));
return true;
default:
return super.onOptionsItemSelected(item);
}

Full code:
res/xml/preferences.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:summary="Select a ringtone"
android:title="Ringtones"
android:key="ringtonePref" />
</PreferenceCategory>
</PreferenceScreen>
Preferences.class
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Your code go here:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
// startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
case R.id.setting:
Intent settingsActivity = new Intent(getBaseContext(),
Preferences.class);
startActivity(settingsActivity);
return true;
default:
return super.onOptionsItemSelected(item);
}
To read these preferences from code, we should create a getPrefs() method, which we can call in the onStart() method. When we call it in the onStart() method instead of onCreate(), we can be sure that the preferences load when we have set them and returned to our main activity,
The getPrefs() method could look like this:
String ringtonePreference;
// Get the xml/preferences.xml preferences
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
ringtonePreference = prefs.getString("ringtonePref",
"DEFAULT_RINGTONE_URI");
androidmanifest.xml
<activity
android:name=".Preferences"
android:label="#string/set_preferences">
</activity>

Yes, you can use SharedPreferences to store the URI of the ringtone the user selected. You can let the user select a ringtone using this:
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Ringtone");
if (mRingtoneUri != null) {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(mRingtoneUri));
} else {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
}
startActivityForResult(intent, RINGTONE_REQUEST);
The above code will prompt the user to select a ringtone from the system. When they select one, you will need to handle the Activity result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RINGTONE_REQUEST && resultCode == RESULT_OK) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
SharedPreferences preferences = getSharedPreferences(PREF, MODE_PRIVATE);
Editor editor = preferences.edit();
if (uri == null)
editor.putString(RINGTONE, null);
else
editor.putString(RINGTONE, uri.toString());
editor.commit();
if (uri == null)
mRingtoneUri = null;
else
mRingtoneUri = uri.toString();
}
}
}
This code will save the URI of the ringtone to SharedPreferences.

Related

SharedPreferences. how do I make sure that the account remains the same?

I have already created users, a name and password in my database. Then the user name is passed to the next activity. And with the next activity from the menu, you can click the logout button and it should display the registration window. But nothing works for me, the "exit" button does not flip to another screen, but just goes into the same one...
mSettings = getSharedPreferences("my_storage", Context.MODE_PRIVATE);
editor = mSettings.edit();
if(mSettings.getBoolean("is_logged", true)){
startActivity(new Intent(Authorisation.this, Menu.class));
finish();
}
editor.putBoolean("is_logged", false).apply();
and after that, I pass the name and password to the editor, set the value to true. Where did I go wrong?
Boolean checkuserpass = DB.checkusernamepassword(user, pass);
if(checkuserpass == true){
Intent intent = new Intent(getApplicationContext(), Menu.class);
intent.putExtra("name", user);
NAME = user;
editor.putString(NAME, user).apply();
editor.putString("PASSWORD", pass).apply();
editor.putBoolean("is_logged", true).apply();
startActivity(intent);
The code of another menu activity. Here I am trying to pass the values to the header (username - name during registration).
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
Intent intent = new Intent(Menu.this, Authorisation.class);
Authorisation authorisation = new Authorisation();
authorisation.log_out();
startActivity(intent);
finish();
return true;
}

Android Studio Beginner;

So here's my code:
public class MainActivity extends AppCompatActivity {
/* access modifiers changed from: protected */
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
Intent intent;
switch (view.getId()) {
case R.id.button: // Text
intent = new Intent(this,"Faiz Ahmed \nLove music forever\nTCSS 450");
break;
case R.id.button2: // Image
intent = new Intent(this, ImageActivity.class);
break;
case R.id.button3: // Web
intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("http://developer.android.com"));
break;
case R.id.button4: // Toast
Toast.makeText(this, "Here's to a new quarter!", Toast.LENGTH_SHORT)
.show();
break;
case R.id.button5: // Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("I am a dialog").setTitle("Some Title").create().show();
}
intent = null;
if (intent != null) {
startActivity(intent);
}
}
}
All the import's are there I just for some reason none of the buttons are working. The first 2 (Text & Image) I know I need a lot more code, but the last 3 should work & they're not for some reason. Any know what I'm doing wrong & how to fix it?
Nothing of the buttons will work because in the switch-case statement you initialize the intent according to which button was pressed.
After that you set intent to null and lastly you start the activity if the intent is not null, but it always is (since you set it before to null).
Short story delete intent = null; statement.
// intent = null; delete this line
if (intent != null) {
startActivity(intent);
}

Shared Preferences not retrieving value

I have two activities namely Registration and Login where I defined a Shared Preferences in the Registration class. On the Registration page, if you click on the button that takes you to the Login Activity from the Registration Activity. I am storing a Shared Preferences value, so that on launching the app the second time, let the app immediately go to the LoginActivity instead of opening the first Activity which is Registration Activity. Here is my SharedPreferences class called Session.
private SharedPreferences prefs;
public Session(Context cntx) {
// TODO Auto-generated constructor stub
prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
}
public void setusename(String usename) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", usename).commit();
editor.commit();
public Boolean contKey(String key){
if (prefs.contains(key)){
return true;
}else{
return false;
}
}
Here is the button in the RegstrationActivity that stores the SharedPreferences value before going to the LoginActivity so that on launch the second time, it opens the LoginActivity class
loginLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
session = new Session(getApplicationContext());
session.setusename("hello123");
Intent intent = new Intent(getApplication(),ActivityLogin.class);
startActivity(intent);
finish();
}
});
I have defined this in the onCreate method of RegistrationActivity class to switch to the ActivityLogin screen on next time the app is launched
System.out.println("it got here...2");
try {
if (session.contKey("username")) {
System.out.println("it got here...");
Intent intent = new Intent(getApplication(), ActivityLogin.class);
startActivity(intent);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
} else {
System.out.println("it got here...3");
Intent intent = new Intent(getApplication(), ActivityRegistration.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.enter, R.anim.exit);
}
}catch(Exception ex){
}
Please why is my Shared Preferences not switching to the LoginActivity the second time the is launched. Kindly assist
TRy this
private SharedPreferences prefs;
public Session(Context cntx) {
// TODO Auto-generated constructor stub
prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
}
public void setusename(String usename) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", usename);
editor.commit();
}
}
public String getusename() {
return prefs.getString("username","");
}
}
than use this way to get value SharedPreferences
session = new Session(getApplicationContext());
session.setusename("hello123");
String username=session.getusename();
Sample code
try {
if (!session.getusename().equals("")) {
System.out.println("it got here...");
Intent intent = new Intent(getApplication(), ActivityLogin.class);
startActivity(intent);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
} else {
System.out.println("it got here...3");
Intent intent = new Intent(getApplication(), ActivityRegistration.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.enter, R.anim.exit);
}
}catch(Exception ex){
}
Instead of default shared preference, you can define a private one with your desired name in the Activity A..like as follows..
SharedPreferences preferences = getSharedPreferences("myPreferences",0);
SharedPreferences.Editor editor= preferences.edit();
editor.putString("userName","raju");
editor.commit();
If you want to retrieve that value from shared preferences in Activty A(I mean in the same activity), you can use following code..
String user_name= preferences.getString("userName","N/A");
or else in Activity B(I mean in some other activty), this time you don't need editor(for retrieval)..the code will be as follows
SharedPreferences preferences = getSharedPreferences("myPreferences",0);
String user_name= preferences.getString("userName","N/A");

Programmatically bypass "touch to configure" Android Widget

I would like to know how to bypass the "Touch to Configure" on my android widget programmatically. Once I have installed the android widget on my device it says "Touch to configure" which then executes my class "IntelligenWidgetConfig.java". But I want this to automatically happen on start up of my application. Can anyone advise me on how to approach this method or any solution that might help?
Android Config Class:
public class IntelliGenWidgetConfig extends Activity {
private static final String TAG = "IntelliGenWidgetConfig";
public IntelliGenWidgetConfig() {
super();
}
public static int getAppWidgetId(Context context) {
SharedPreferences settings = context.getSharedPreferences("Intelligen", MODE_PRIVATE);
return settings.getInt("appWidgetId", 1);
}
protected void setAppWidgetId(int id) {
SharedPreferences settings = getSharedPreferences("Intelligen", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("appWidgetId", id);
editor.commit();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the result to CANCELED. This will cause the widget host to cancel
// out of the widget placement if they press the back button.
setResult(RESULT_CANCELED);
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
setAppWidgetId(extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID));
}
// If they gave us an intent without the widget id, just bail.
if (getAppWidgetId(this) == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
final Context context = IntelliGenWidgetConfig.this;
RegisterForPushNotification(context);
// Push widget update
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.intelligen_widget_layout);
appWidgetManager.updateAppWidget(getAppWidgetId(this), views);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, getAppWidgetId(this));
setResult(RESULT_OK, resultValue);
finish();
}
private void RegisterForPushNotification(Context ctx) {
Intent registerIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registerIntent.putExtra("app", PendingIntent.getBroadcast(ctx, 0, new Intent(), 0));
registerIntent.putExtra("sender", "209845001369");
startService(registerIntent);
}
private void DeregisterFromPushNotification(Context ctx) {
Intent unregisterIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregisterIntent.putExtra("app", PendingIntent.getBroadcast(ctx, 0, new Intent(), 0));
startService(unregisterIntent);
}
}
AndroidManifest.xml (section):
<activity android:name=".IntelliGenWidgetConfig" android:label="#string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
XML
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="180dp"
android:minHeight="360dp"
android:updatePeriodMillis="0"
android:initialLayout="#layout/intelligen_widget_layout"
android:resizeMode="vertical"
android:minResizeWidth="280dp"
android:minResizeHeight="70dp"
android:configure="za.co.infotech.www.intelligen.IntelliGenWidgetConfig"
android:previewImage="#drawable/preview">
</appwidget-provider>

Android new activity not starting

I wrote this code following the skeleton of Reto Meier's "Professional Android 4 Application Development" and some slide of my professor, but i can't understand why the new activity (PreferencesActivity, fully coded) is not starting and is not raising any kind of errors: in the VM it just won't do anything when i press "Preferences" in the standard android menu I created.
I added the new activity in app's manifest correctly (just name, label, theme and screen orientation).
Here's the code
public class MainActivity extends Activity implements OnClickListener, OnValueChangeListener {
static final private int MENU_PREFERENCES = Menu.FIRST+1;
...
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_PREFERENCES, Menu.NONE, "Preferences");
return true;
}
public boolean onOptionsitemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case (MENU_PREFERENCES): {
Intent i = new Intent(this, PreferencesActivity.class);
startActivity(i);
return true;
}
}
return false;
}
...
}
The only strange thing I get is this warning in Logcat
06-20 14:50:49.760: W InputManagerService(699): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#41219950
You can use both of them
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
Intent i = new Intent(MainActivity.this, PreferencesActivity.class);
But it's better to use 1st one because in 2nd one memory leakage problem may occour and also just add this line in your manifest file.
<activity android:name=".PreferencesActivity" />
Your Code :
Intent i = new Intent(this, PreferencesActivity.class);
startActivity(i);
return true;
Instead of this you need to pass MainActivity.this
Intent i = new Intent(MainActivity.this, PreferencesActivity.class);
startActivity(i);
return true;
Issue is Proper context is not passing so its not starting Activity.
Instead of using this you could use getApplicationContext(), it gets you the context of the application object for the currents process.
Try this....
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
startActivity(i);
This May Help You..
You need to pass MainActivity
Intent i = new Intent(MainActivity.this, PreferencesActivity.class);
startActivity(i);
return true;
Better to use menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected()");
switch (item.getItemId()) {
case android.R.id.yourId:
finish();
return true;
case R.id.Yourid:
return true;
default:
return super.onOptionsItemSelected(item);
You can also write
startActivity(new Intent(getApplicationContext(),NextActivity.class));
write your activity name in NextActivity.class

Categories

Resources