I have asked a question before 2 days about daily notification and i put my code and i didn't get any useful answer so i need someone write a code that show notification every 1 hour .
caution (i have write a code doing that but i was have a problem that is every time i open the activity it gives me a notification i don't need that i just want it give a notification at the first time i run app or press a button to start it then the service runs to give notification every 1 hour ) .
Hope anyone can help me .
Thanks.
Well this seems like a two part question, for one the effect that you want to wait and be able to check to start the notifications and to save that value. SharedPrefence's can do this, you could save a boolean value to see whether or not a alarm should be set. Easy enough to access.
public boolean saveSharedBoolValue(String key, Boolean value, Context localContext) {
SharedPreferences sp = localContext.getSharedPreferences("FILE_NAME", 0);
Editor edit = sp.edit();
edit.putBoolean(key, value);
return edit.commit();
}
public Boolean getSharedBoolValue(String key, Context localContext)
{
SharedPreferences sharedPreferences = localContext.getSharedPreferences("FILE_NAME", 0);
Boolean value = sharedPreferences.getBoolean(key, false);
return value;
}
Something like this. With alarm notifications, that start on startup I had this similar issue. What I noticed was that I was creating an alert for earlier than my present time and it created a single notification automatically.
That may not be your issue though,
calendar.setTimeInMillis(System.currentTimeMillis() +5*1000);
this line from your code. Isn't it essentially creating an alarm just right after it starts? 5000 milli seconds aren't much after present time.
Related
I write an application, I want to get a phone number, but getline1number() not working on any devices.
So, I want to create a pop-up to enter a phone number and submit to save and don't show in next time open app.
Like this:
You can always use SharedPreferences to do such things:
SharedPreferences sp = getSharedPreferences("FirstTimeFile", Context.MODE_PRIVATE);
/**
* when the app is opened for the first time, no such variable
* (appIsOpenedForTheFirstTime) exists. So, it becomes true.
*/
boolean appIsOpenedForTheFirstTime = sp.getBoolean("IsAppOpenedForFirstTime",true);
//since it is true, it will be set to false after the execution of following block:
if(appIsOpenedForTheFirstTime) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("IsAppOpenedForFirstTime", false);
editor.commit();
//PUT THE CODE FOR YOUR POPUP HERE
}
As the SharedPreferences values remain in the application data even after you close the app, so the next time you open the app, the value of appIsOpenedForTheFirstTime will be false and hence your pop-up code won't be executed.
Ah, as a side-note, if you clear the app data, everything gets cleared - including the SharedPreferences. Read this official article for in-depth understanding.
I am trying to develop an app that requires certain values to be set by the users at the app's first startup only, because i don't wanna bother them frequently inputting the values everytime they launch the app. My app has a single activity main and uses certain values that are inputted by the users at first startup. How can I make this possible.
Please explain me elaborately . :-)
You should use SharedPreferences to keep a track of the first use.
In the onCreate Method of your Activity (Startup activity), you could do something like this,
SharedPreferences userPrefs = getSharedPreferences("UserPrefs", 0);
Boolean firstUse = userPrefs.getBoolean("firstUse", true);
if(firstUse){
//this implies it is the first use of the app
//also once you are done implementing the logic for first use you need to put firstUse as true
SharedPreferences.Editor editor = userPrefs.edit();
editor.putBoolean("firstUse", false);
editor.commit();
}
else{
//take the user directly inside the app
}
Also, if you plan to save user information in the first use, look at different ways of storing data here.
show the alert initially and after getting the input values keep it in preference and next time check whether the required values existing or not. If it is already there avoid popup
For getting more information about shared preference check this link http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
preferences_statusFirst.getString("boot", "");
if (status.length() <= 0)
{
showDialog(DIALOG_birth);
editor_boot.putString("boot", "1");
editor_boot.commit();
}
else
{
}
}
I want my application can get user's input of time (HH:mm) from EditText widget.
Based on this time value my app needs to show a dialog when current time matches entered time.
Gaauwe
*Edit*
I want to place an EditText widget in my app.
A user will fill it with some time value (e.g. 10:30).
Then when real time (10:30) come up a dialog will be shown.
I think you can use the AlarmManager for this.
I d suggest you have a look at some tutorials like these to help you get started
http://michael.theirwinfamily.net/articles/android/android-creating-alarm-alarmmanager
http://android.arnodenhond.com/tutorials/alarm-notification
That is not too difficult. When user finished editing you EditText, read the time value and create instance of AlarmManager with start time calculated as difference between current time and whatever user wrote in the EditText. Better to use TimePicker to avoid parsing user`s input. Add receiver for you AlarmManager, receiver will start Service which will show dialog or do anything you want. You need to use AlarmManager because if your device is sleeping nothing will wake it up except system call like AlarmManager. #Zortkun 's post with links will help you to figure out how manage AlarmManager.
try this :
use the service : then when user enter time starts a service when system time and user entered time match the shows..
You can pull the data out of the EditText with:
findViewById(R.id.yourEditText).getText().toString();
The rest of your question I didn't understand.
RAW WAY!
So when user put text inside edittext and click button, you could save text in this way:
String time = findViewById(R.id.yourEditText).getText().toString();
and start a thread that check for time, and when time is equal to user's string time, you can show a dialog :)
Thread t = new Thread(new Runnable(){
public void run(){
while(new Date().getLocalTime()!=usersTime){ // is just pseudocode
Dialog.show();
}
}
});
I'll try to understand...
Seeing as you know how to pull the text from an EditText, you'll need an if statement.
Something that compares that time to the current time.
if (editTime == realTime) {
Toast.makeText(getApplicationContext(), "RING RING RING",
Toast.LENGTH_SHORT).show();
}
Use something like this:
Read this to figure out how to get a string of current time.
OK, so I'm playing around with an android app.
The 90% use case is that users want to go straight to the primary list screen to find what they're looking for. That's what I want as my default screen.
The first time a user loads the app however, some configuration is required before their list screen is of any value to them.
So my question, is how I can go about displaying the configuration activity the first time the app is opened up, and then the list screen for future openings.
I also want to put a demo button on the configuration screen, so I suppose more than just detecting that it's the first time, I specifically want to detect whether the user has performed certain configurations within the first screen.
After the first time the user has loaded the app you could store the details of whether user has performed the configurations in SharedPreferences.
protected void storeSharedPrefs(String value) {
/*
* Storing in Shared Preferences
*/
editor.putString("first", value);
editor.commit(); //Commiting changes
}
Check each on time application is loaded, whether its the first time and configuration details has been entered correctly by checking SharedPreferences
private boolean first_time_check() {
/*
* Checking Shared Preferences if the user had pressed
* the remember me button last time he logged in
* */
String first = uPreferences.getString("first", null);
if((first == null)){
return false;
}
else
return true;
}
i like dweebsonduty's method. a similar way to do this is to save their configuration information in files on the SD card. your app could start out by checking for those files and loading the information. if the load is successful, it moves on to the next activity, if not it starts the appropriate activity to prompt the user for input.
I have done this same thing, but instead of swiching activities i just switch views until i have all the info i need, and then move on.
Many applications actually store the current version in SharedPreferences, and check against it for if an update has been installed. Its a clever way of achieving a "what's changed" popup, or making sure that some settings get set (I would be wary of just having a boolean flag because if you ever want to add an additional setting, you will need a second flag and it gets messy after the third, etc.).
String VersionValue = "v.1.0";
final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean(VersionValue, true)) {
//the app is being launched for first time, do something
NewDialogFragment newFragment = new NewDialogFragment();
newFragment.show(getFragmentManager(), "New");
// record the fact that the app has been started at least once
settings.edit().putBoolean(VersionValue, false).commit();
}
You could do it this way and still get the same result I tried it its a small workaround if u do not fully understand how to check if the app is updated. Instead with this code you can just simply change the String VersoinValue to your current app version and android will think the app is a new first time app and will only display the code u wrote once until you change VersionValue on your next update. (:
How will you be storing the configuration?
If it is in SQLlite you could just create a table called firstuse and put a field in there called hasbeenused and make it null. Then when the app is used you can put a 1 in there. Then you can read it each time your app loads and if that field = 1 then go to your next activity.
Hi I don't want to display the any notification service in status bar if i saw one notification service once .For example i am displaying persons who are exceeding the 20 km distance from my location .some persons are displayed.when i saw it once then automatically the icon in the status bar is don't displayed.For this one give me some suggestions .Thanks in advance.
If your question is about preventing the display of notifications once the user clears one of your previous notifications, you'll probably need to maintain your own data structure to monitor this.
The idea is:
Store a hashtable/hashset/other data structure indicating which notifications the user has already seen.
Before showing a notification, check the hashtable – if the notification is in there, don't show it. Otherwise, show it.
When showing a notification, add it to the hashtable.
Flush the hashtable every so often.
You might also want to look into Notification.deleteIntent.
Caution: Before doing this, consider if this is really necessary. It might be sufficient to simply collapse visible notifications by reusing notification IDs.
I do this:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean used = sp.getBoolean("notif_used", false);
if ( used )
return;
else {
/* show the notification */
Editor editor = getSharedPreferences().edit();
editor.putBoolean("notif_used", true);
editor.commit();
}