I want to access a variable present in a service from another service/an activity....
Can anyone give an idea?
To communicate between two service or activity, you need to use AIDL
It is not really difficult to do, and there is a lot of tutorial like this.
You can make a public getter for that variable in your Service class, bind to that service, and access the getter to give you that variable.
If what you mean is that you want to access the variable after you close and open the app, then you're probably looking for SharedPreferences. Note that this requires a context (an activity or service).
To store:
int data = 5;
SharedPreferences storage = getSharedPreferences("storage", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = storage.edit();
editor.putInt("myInt", data);
editor.commit();
To get:
SharedPreferences storage = getSharedPreferences("storage", Context.MODE_PRIVATE);
int data = storage.getInt("myInt", 0);
Related
I have two applications -App 1 and App 2
I want to access App2 data from App1.
However, I don’t want a complex database structure, I just need to read couple of boolean flags from App1.
One way is to use content provider, where I will need to define table or some structured data.
Intent will be also not very useful as I don't want to launch app instantly after affecting any changes
Any other relatively less complex workaround?
(I don't want it to bind it to any adaptor or anything. Assume two boolean value in SharedPreference : Value1 & Value2 in App2 and I wish to access it via App1)
first app:
SharedPreferences prefs = getSharedPreferences("test_prefs",
Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("pref_key", true);
editor.commit();
second app:
/* where com.example is the first app containing the preferences */
Context myContext = createPackageContext("com.example",Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences testPrefs = myContext.getSharedPreferences
("test_prefs", Context.MODE_WORLD_READABLE);
boolean data = pref.getBoolean("pref_key", false);
Right way to do it is to create a service on App1 and bind to this service from App2.
Here is a link for you reference.
https://stuff.mit.edu/afs/sipb/project/android/docs/guide/components/bound-services.html
I am new in Android Development. I am trying my hands on developing an alarm app. When I set the alarm, I use a TextView to show the time for which the alarm in set up(initially empty). But when I close or minimize the app and start it again the TextView is again empty. How to get rid of this?
I looked for its solution in android app development manual, but still couldn't find my way out.
Particularly visiting developer.android.com will help you to get started with android development. Anyways you can either use any of the following to save your data:
SharedPreferences
Sqlite Database
File (Pertaining to your app's location)
And when you are reopening your application you can retrieve the information from this methods.
Saving data can be done easily with the SharedPreferences.
private final String SAVED_ALARM_TIME_KEY = "SavedAlarmTime"
private final String ALARM_PREFERENCES = "AlarmPreferences"
private void saveAlarmTime(Context context, long alarmTimestamp) {
SharedPreferences sharedPref = context.getSharedPreferences(ALARM_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong(SAVED_ALARM_TIME, alarmTimestamp);
editor.commit();
}
private long getAlarmTime(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(ALARM_PREFERENCES, Context.MODE_PRIVATE);
return sharedPref.getLong(SAVED_ALARM_TIME, 0);
}
This is great for something like user settings in the application. But I imagine you will later want to add multiple alarms to your application. In this case it would be better if you used a database. It will provide you with more options to scale the implementation. For instance you wish to add a functionality for repeating alarms at certain days the database will come in very handy.
You can refer to the documentation: https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
I have question for receive value in smartwatch. Currently I follow this steps from this question
Actually,the person who ask it has the answer how to do that, but since my reputation for comment is not enough, so I can't ask question by comment in his/her question.
Right now, based on Mr. Eir,the person who answered the question. I have problem in what he answered:
You also want to pass some arguments to your Extension, i.e. the
String you mention. This can be a bit tricky; normally, you would pass
that String in the Intent itself, as an extra, but here, that is not
available. You need to save that information (the String) on a
location that your Extension can access as well. So, if your Activity
and your Extension are part of the same app, that location can be the
app preferences: the Activity saves the value in the preferences, and
the Extension reads it from the same preference and displays it on the
SmartWatch or whatever.
He said that I can save the value in preference and the Extension reads it from the same preference and displays it on the SmartWatch. Unfortunately, I don't know how the extension reads it. I have try to put the value in samplepreferenceactivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences("SHARED_PREFS_FILE",0);
String message = settings.getString("send", "message");
}
I don't know how to put the value in controlextension class, If it possible to put, I want to you use for changing "Hello watch". Below you can find controlextension class:
public class HelloWatchExtension extends ControlExtension{
...
public HelloWatchExtension(Context context, String hostAppPackageName) {
super(context, hostAppPackageName);
width = getSupportedControlWidth(context);
height = getSupportedControlHeight(context);
layout = new RelativeLayout(context);
textView = new TextView(context);
textView.setText("Hello watch!");
textView.setTextSize(9);
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.WHITE);
textView.layout(0, 0, width, height);
layout.addView(textView);
}
Since it is not activity, so it is n't possible to take by using getpreference. Anybody knows how?
"Since it is not activity, so it is n't possible to take by using getpreference. Anybody knows how?"
You can access the preferences through context:
context.getApplicationContext().getSharedPreferences(...);
A few pointers about using shared preferences:
SharedPreferences preferences = _context. getApplicationContext().getSharedPreferences("com.example.AppName", Context. MODE_MULTI_PROCESS);
Putting string in shared preferences:
_preferences.edit().putString(“OBJECT”, “object_name”).commit();
Retreaving string from shared preferences:
_preferences.getString(“OBJECT”, "default_name");
If you are just trying to pass a string between an Activity in your project and your ControlExtension you don't need to use SharedPreferences. The easiest way is to just register a dynamic BroadcastReceiver in your extension and broadcast an Intent from the Activity passing your string inside the Intent.
I'm developing an app that has to share strings between activities. I'm trying to get the seperate activities to call a public class with set and get methods. The calling the methods part works and I manage to get a response although the set value has to be rememberd by the set and get class. Here's a link to my set and get class, it's pretty basic: http://pastebin.com/0WabNKz3
Now my question is this: How do I make the set and get class to remember my values between sessions? Feel free ask questions if there's anything you didn't understand.
Thanks!
You need to use SharedPreferences. That's the way to save data even after the app is closed and you can access it from anywhere:
public void savePrefrences(String key, String value)
{
SharedPreferences prefs = context.getSharedPreferences(context.getPackageName(), 0);
prefs.edit().putString(key, value).commit();
}
public String getPrefrences(String key)
{
SharedPreferences prefs = context.getSharedPreferences(context.getPackageName(), 0);
return prefs.getString(key, "");
}
Save the prefrence when and whereever you want and get it whenever and from wherver you want.
The value will not delete when you close the app.
I ended up creating invisible EditTextPreference that now hold the data that I want to keep because they can be shared easily.
When you say saving between sessions, do you mean between the app being paused, or closed completely?
A good resource for lifecycle and storing data across sessions is:
//developer.android.com/training/basics/activity-lifecycle/index.html
i have created a live wallpaper and in that there is a "setting" button which loads PreferenceActivity but without clicking on "settings" but i want to access the SharedPreferences within subclass of Engine or WallpaperService. As i just want to access the small single string so i don;t want user to go into settings and access that string.
So i want to execute this code inside Subclass of Engine or WallpaperSerivce
SharedPreferences mPrefs = getPreferenceManager().getSharedPreferences();
String option = mPrefs.getString(
this.getResources().getString(R.string.name),
this.getResources().getString(R.string.option));
It is not the best way to do it I'm sure but I use getters and setters to achieve this effect.
private int mySetting = defaultvalue
public int getMySetting() {
return mySetting;
}
public void setMySetting(int mySetting) {
this.mySetting = mySetting;
}
I obviously used some plain text in that code but hopefully it is pretty self explanitory
You set this variable while in Settings Class with...
Settings.this.setMySetting(value);
Remove "this" to call from other classes
You can retieve this information in any of your classes using the following
Settings.getMySetting();
You can use pretty much any variable type just make sure you define the mySetting variable as that type before trying to pass a value other than int as in this example. Hopefully this helps.