Hello, I'm starting with Android and I'm trying to program a timer which launch an application or an AlertDialog, even if the application is in background. I know it is very invasive for the user but is necesary cause it show an important alarm that must be handled instantly.
Thats the code I used, but only works if MainActivity is the current activity =(
Code:
Timer time=new Timer();
teme.schedule(new TimerTask() {
public void run() {
Intent dialog = new Intent(MainActivity.this., Alarm.class);
dialog.setFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(dialog);
}
}, delay);
Can someone help me please?
Going to have to run it as a Service
You have MainActivity.this is why MainActivity has to be running.
When you start an activity, update the reference, and then call:
[whateverActivityIsRunning].this
Aditionally, you may try launching another thread under your process, this however will not work if your app was force closed by system or user.
Code:
private void dodelayed(int delay, Intent someintent){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override public void run(){
//If that dialog is custom class then someintent.setclass(this,classname.class);
startActivity(someintent);
}
},delay);
}
Thanks guys, starting the activity from a service works nice for my target.
Now i'm trying a new thing:
I start the service in my app, then i open another app, Angry Birds for example xD. The startActivity tiggers and my app comes to front with a Activity that shows a message and a button.
I want a button click hide the Activity and move Andry Birds back to front.
How could I do it? Thanks!
finish();
With finish() I go to the last Activity of my app before it went to background =(
Do you know what that last activity is? You could set a variable in it to call from your other activity telling it to also finish.
You could also try something like this:
http://developer.android.com/guide/topics/manifest/activity-element.html#reparent
The "Love & Hate" of Android OS.
finish(); will only pop the last Activity on it's OWN thread and not Angry Birds as it is running on it's own thread (well VM actually).
The OS was designed this way; they didn't want any old application dumping something else that was running...makes sense to me.
I don't know what you're doing, but it sounds like it's working OK to me. Say I'm in the middle of creating a text, your timer goes off (for whatever reason I don't know), I respond to it and go about my texting. I would be more than upset if your timed event blew me out of my texting!!! Your app would be in the trash bin pretty quickly.
Might need a little more info on what you're trying to do...
My app wont be comercial. It reads from a bluetooth device checking for alarms, if any, gives the user some time for cancell it, else its sent to the main server.
I want do something like whatsapp application. where, if you activate the emergent notifications. when you receive an message a alertdialog is shown. But you can cancell it and continue with your stuff
See that screenshot:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
for such notifications, first create an activity with normal textview and buttons and remember to use layout_width and height as wrap_content, then in your application manifest use the following
Code:
<activity android:name="activity"
android:label="Title"
android:theme="@android:style/Theme.Dialog">
now when you call this activity from a service, a dialog box such as in the picture above depending on your layout xml will be shown. and when you call finish() through a button on this dialog, the focus will be passed to angry birds since this activity was launched from a parent with no UI.
Exactly what i was looking for. Thanks!
dont waste posts, hit the thanks button!
Sorry, you are limited to five thanks per day
have to wait =)
Dont worry I gave him one for ya
Related
Hi guys!
I want to create a simple app, i have tried to, but i fail every time (in the very start!)
It shall be built in this way:
When opened a list will appear. When an element is chosen another list will appear.
In the second list, when an item is selected text will appear!
Can some very nice chap help me?
What have you tried so far? Are you developing in eclipse and java? Maybe try the Google App Inventor first.
You are basically wanting an app with a bunch of listviews (class extends listview activity) and finally a standard activity with a textview.
That should be pretty simple but then you have to populate the lists with some kind of data.
The best way to start learning is to just start, and keep at it. Google is your friend, every question you have, google it. There is a ton of resources on the net for android already. Chances are your question has been asked by someone else.
If you are wanting someone to do it all for you...good luck. Unless you pay someone to do it.
I have builded my own j2me midlet with microemulator, looks good, but i can't enter any element in any listview. Guess it is because one could not use the center key in the midlet but had to use the to other keys. You know, just below the screen on good old phones.
You're putting a lot into a "simple" app in my opinion; especially if you're new to programming.
Restart a new project. Create an array of data. Create array list and loop through your data array and add the items to the array list. Create an array adapter that will get populated with your list. Setup main.xml with <ListView> tag.
If you get that far, your list should be displayed. Then work on your onClicks'. It's going to get confusing if you're going to have a clickable listview come up that's clickable and does something else. Why not just one list?
Sounds very hard :S how about creating a kind of book with chapters, so one can start up the app and choose a chapter and be sent directly to it? Alternative for the lists.
Well, you have to deal with "onClicks" no matter whether they are buttons or items in a listview.
Here's a little code to help you out with a listview:
Code:
// Create some data
String myArray[] = {"Chapter 1", "Chapter 2", "Chapter 3"};
// Create a list to hold our data
ArrayList<String> myList = new ArrayList<String>();
// Load the list; usually done from an array in a loop
for(int i=0; i<myArray.length(); i++) {
myList.add(myArray[i]);
}
// Setup Array Adapter with myList
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.test_list_item, myList);
// Get id of ListView in xml file
lv = (ListView) findViewById(R.id.myListView);
// And display it
lv.setAdapter(aa);
Typed that off top of my head so I might not be 100%; compiler will let you know LOL Once you get a list displayed, Google something like "Android ListView onClick" for further help.
This code is pretty basic; if you don't understand it, you may want to look into some tutorials on Android programming.
Good luck
Rootstonian said:
Well, you have to deal with "onClicks" no matter whether they are buttons or items in a listview.
Here's a little code to help you out with a listview:
Code:
// Create some data
String myArray[] = {"Chapter 1", "Chapter 2", "Chapter 3"};
// Create a list to hold our data
ArrayList<String> myList = new ArrayList<String>();
// Load the list; usually done from an array in a loop
for(int i=0; i<myArray.length(); i++) {
myList.add(myArray[i]);
}
// Setup Array Adapter with myList
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.test_list_item, myList);
// Get id of ListView in xml file
lv = (ListView) findViewById(R.id.myListView);
// And display it
lv.setAdapter(aa);
Typed that off top of my head so I might not be 100%; compiler will let you know LOL Once you get a list displayed, Google something like "Android ListView onClick" for further help.
This code is pretty basic; if you don't understand it, you may want to look into some tutorials on Android programming.
Good luck
Click to expand...
Click to collapse
Dont forget your setcontentview()
I said "off the top of my head" LOL
I usually don't provide code, but I was in a good mood. There is still plenty of work to be done around that snippet I posted...as you well know
#¤&*@ 1 buck for the man who will write the 'skeleton' of this app!
Ihaveatattoo said:
#¤&*@ 1 buck for the man who will write the 'skeleton' of this app!
Click to expand...
Click to collapse
why are you asking for a how to if you are not willing to put any effort in it ?
i dont get it ...
It's about two months ago i asked for help. I the meantime i have tried to make it. But thanks, i just remembered i had some code to work from!
The problem is, i can almost no java. And i have another study and work.
At least post the code you have so far. You are asking for help with an app you have barely described with code we have never seen.
Added my code snippet to empty project, added listview to main.xml. Took me longer to remember my photobucket password! LOL
Now, if you're not a programmer, there isn't much we can do to help besides writing it for you. And we like to get paid for that
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
I know how to make that.. ;-) Problem now is to make the items in menu "click able ". I'LLC post my code soon.
Thanks for help chaps!
As stated in the subject, I'd like to implement in my project that little auto-dismissing text popup that sometimes appears when applying an action in certain apps.
It's a simple floating text with a dark rectangle as background positioned in the bottom part of the app that remains between activities/application switches and goes away automatically after X seconds.
Does anybody know how it's called?
It was called Toast.
You can easily custom a toast to have a interface like whatever you want, look Titanium Backup for example.
Default usage like below:
Code:
Toast.makeText(mContext, "Hello", Toast.LENGTH_LONG).show();
As a side note, the App-Msg(Crouton) library provides a good alternative to toasts : in that it is context-centric, the message will only be displayed on the activity/fragment it was generated from, whereas if the user quits your app while a toast is displayed, for example, the toast will be displayed on his launcher or on he app he switched to, which could be confusing.
Its usage is also very simple :
Code:
AppMsg.makeText(/*Activity*/, /*CharSequence*/, /*AppMsg.Style*/).show();
Maybe this library can be helpful on my app. I have a custom toast, which still displayed after I choose to leave the activity via a button. So with this library the toast dissapear when the activity goes to background? And also can customize the look of toast?
dancer_69 said:
Maybe this library can be helpful on my app. I have a custom toast, which still displayed after I choose to leave the activity via a button. So with this library the toast dissapear when the activity goes to background? And also can customize the look of toast?
Click to expand...
Click to collapse
Yeah exactly.
And yes, you can modify the appearance of the toast to a certain extent (background color, gravity and display length afaik) using the static methods already setup in the library, but nothing prevents you from forking the library and modifying it to suit your needs.
You can also choose to have a toast which will only be dismissed when the user clicks it.
There are 3 types of preset styles for the croutons (from left to right in the image below) :
AppMsg.STYLE_ALERT (red) AppMsg.STYLE_CONFIRM (yellow) AppMsg.STYLE_INFO (green)
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
But you can create your own using something like this :
Changing length & color :
Code:
AppMsg.Style style = new AppMsg.Style(AppMsg.LENGTH_SHORT, R.color.holo_blue); [COLOR="SeaGreen"]// the default length is [I]AppMsg.LENGTH_LONG[/COLOR][/I]
colors.xml :
Code:
<resources>
<color name="holo_blue">#33B5E5</color>
</resources>
Changing gravity :
Code:
appMsg.setLayoutGravity(Gravity.BOTTOM); [COLOR="SeaGreen"] // the crouton is displayed at the top by default[/COLOR]
Result in the thumbnail
Thanks to Evgeny Shishkin for his work on this library by the way.
Hey guys,
I'm still learning to make android apps and now I'm writing small apps...Today I wrote a small app to test the scrolable tabs+ swipe created with blank activity.I need to show show contents of a PreferenceScreen xml.So I created a xml and added a new class:
Code:
public class About extends PreferenceActivity{
[user=1299008]@supp[/user]ressWarnings("deprecation")
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.info);
addPreferencesFromResource(R.layout.info);
}
}
And then found that the below code is responsible for the screens and so done this:
Code:
[user=439709]@override[/user]
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
Intent intent = new Intent();
intent.setComponent(new ComponentName(
"com.example.testy","com.example.testy.About"));
startActivity(intent);
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
Now the thing is that,I need the app to show the contents of xml directly on section1 instead of opening an activity above.Also noted that,when I go to section2,The activity is opened once and when back to section1,twice.
I need to show completely different contents on section1 and section2 also that the contents should be directly on sections instead of opening a new activity over the main one.Sorry for confusing you.All the xml I use are in preferences.Thank you
Regards,
Vijai
vijai2011 said:
Hey guys,
I'm still learning to make android apps and now I'm writing small apps...Today I wrote a small app to test the scrolable tabs+ swipe created with blank activity.I need to show show contents of a PreferenceScreen xml.So I created a xml and added a new class:
Code:
public class About extends PreferenceActivity{
[user=1299008]@supp[/user]ressWarnings("deprecation")
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.info);
addPreferencesFromResource(R.layout.info);
}
}
And then found that the below code is responsible for the screens and so done this:
Code:
[user=439709]@override[/user]
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
Intent intent = new Intent();
intent.setComponent(new ComponentName(
"com.example.testy","com.example.testy.About"));
startActivity(intent);
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
Now the thing is that,I need the app to show the contents of xml directly on section1 instead of opening an activity above.Also noted that,when I go to section2,The activity is opened once and when back to section1,twice.
I need to show completely different contents on section1 and section2 also that the contents should be directly on sections instead of opening a new activity over the main one.Sorry for confusing you.All the xml I use are in preferences.Thank you
Regards,
Vijai
Click to expand...
Click to collapse
Change your activity into a FragmentActivity. Then you will be able to add a PreferenceFragment. However, you will need to show the content of your screen in a Fragment instead of the "normal" way of doing it in the onCreate method. Add a Fragment there and if you want to show the preferences, add a new one on the top. It will not open a new Activity but just a new Fragment in the Activity.
nikwen said:
Change your activity into a FragmentActivity. Then you will be able to add a PreferenceFragment. However, you will need to show the content of your screen in a Fragment instead of the "normal" way of doing it in the onCreate method. Add a Fragment there and if you want to show the preferences, add a new one on the top. It will not open a new Activity but just a new Fragment in the Activity.
Click to expand...
Click to collapse
Wait...I dont understand this:
However, you will need to show the content of your screen in a Fragment instead of the "normal" way of doing it in the onCreate method. Add a Fragment there and if you want to show the preferences, add a new one on the top. It will not open a new Activity but just a new Fragment in the Activity.
Click to expand...
Click to collapse
.However I changed my activity to fragment activity from the link you gave
vijai2011 said:
Wait...I dont understand this:.However I changed my activity to fragment activity from the link you gave
Click to expand...
Click to collapse
Yes, change it to a FragmentActivity. It will act like the container of your Fragments. Then you will set the content by adding new Fragments on the top of each other.
Check this: http://developer.android.com/guide/components/fragments.html
Here´s a full example showing how to implement Tabbed Preferences Screen (with no swipe for now). I hope it helps you!!
Entire ICS source code attached!
Screenshots:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
I have the Android SDK and Eclipse downloaded and I can get it work properly. However, there doesn't seem to be any commands anywhere, like, am I supposed to know all of the JAVA commands by memory to use this tool???
I only want a very simple app that does this:
- When screen is off, turn WIFI off
- When screen is on, Turn WIFI on
That's it. I don't need any fancy menu or anything, it can run in the background for what I care, it's just that Stamina mode doesn't work with these roms and bootloaders and whatever, so I need this app to save me some power.
I reckon it'd be pretty easy for anyone who knows java, so maybe I could get someone to post a code for me?
Thanks in advance
You don't need to know everything by memory but you do need to know the basics.
For example, you need to know the difference between an Activity and a Service and determine which one you would want to use.
For your app, you would need a Service and a BroadcastReceiver.
Your BroadcastReceiver would capture the intents of the screen turning on and off:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
You would then call your service from your broadcastreceiver to do the work of turning off your wifi:
http://stackoverflow.com/questions/8863509/how-to-programmatically-turn-off-wifi-on-android-device
Be sure to add the proper permissions in your manifest file that allows you to read and change wifi state.
Ok, so now I know how the code should be, but there's just one more thing.
How in the world can anyone program a user interface with text only??? This program is certainly not easy to use, I don't even know where to put the code with all of those different folders.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Do I even need all of those commands/folders? I tried deleting all that stuff and putting in the code but I got lots of errors obviously.
So if you would please tell me:
- Where to put the code
- How to make an interface like this one with radio buttons or something, a switch might be easier
I don't want anything fancy, just a super basic app
I suppose the code would look like this:
Code:
package selecm.wwf; //your package name
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
WifiManager wm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public class ScreenReceiver extends BroadcastReceiver {
// THANKS JASON
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wm.setWifiEnabled(false)
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wm.setWifiEnabled(true)
wasScreenOn = true;
}
}
}
And then make these in the manifest.xml file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Am I right?
Your user interface is located in the "res/layout" folder. It is most likely called activity_main.xml.
Also, I would recommend keeping your BroadcastReceiver separate from your activity. It makes things cleaner to debug and easier to build on.
After doing some playing and spending more time on this then I planned, using a service does not work reliable with the broadcast receiver. I am not sure how logical google was with the thought process of requiring some intents that can only be registered in code. With that limitation, your activity would need to be running to receive the intent from the screen being turned off or on.
Hello,
I want to set the BackgroundColor of ListView items, but I want to keep the color which appears when I click the items.
How can I do that?
Auroratic said:
Hello,
I want to set the BackgroundColor of ListView items, but I want to keep the color which appears when I click the items.
How can I do that?
Click to expand...
Click to collapse
Rather than replace a "stateListDrawable" with a color or static drawable, replace it with another state list that has the "states" you require
deanwray said:
Rather than replace a "stateListDrawable" with a color or static drawable, replace it with another state list that has the "states" you require
Click to expand...
Click to collapse
I've got it, but I have one problem: The colour on state_pressed should be the default system colour (this one: android.R.drawable.list_selector_background)
How can I do that?
Auroratic said:
I've got it, but I have one problem: The colour on state_pressed should be the default system colour (this one: android.R.drawable.list_selector_background)
How can I do that?
Click to expand...
Click to collapse
I'm not sure what your asking, if you have read about stateListDrawabes and have understood it, also maybe even looked at examples, then you should know ? Open that resource, or even clone it and edit it how you want.
deanwray said:
I'm not sure what your asking, if you have read about stateListDrawabes and have understood it, also maybe even looked at examples, then you should know ? Open that resource, or even clone it and edit it how you want.
Click to expand...
Click to collapse
I have the following:
Code:
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
android.R.attr.state_pressed
}, new ColorDrawable(android.R.drawable.btn_default));
stateList.addState(new int[0], new ColorDrawable(Color.YELLOW));
textView.setBackgroundDrawable(stateList);
The background is yellow, but when I press on the textView, the background turns into transparent (it should be like this when i press:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
)
Auroratic said:
I have the following:
Code:
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
android.R.attr.state_pressed
}, new ColorDrawable(android.R.drawable.btn_default));
stateList.addState(new int[0], new ColorDrawable(Color.YELLOW));
textView.setBackgroundDrawable(stateList);
The background is yellow, but when I press on the textView, the background turns into transparent (it should be like this when i press:
)
Click to expand...
Click to collapse
well fyi you should look at the view type "states" as not sure you want android.R.attr.state_pressed ... neways you probably want state_focused and state_enabled for an editText view, but really google should provide you with simple absolute answers
hope that clears it up neways
deanwray said:
well fyi you should look at the view type "states" as not sure you want android.R.attr.state_pressed ... neways you probably want state_focused and state_enabled for an editText view, but really google should provide you with simple absolute answers
hope that clears it up neways
Click to expand...
Click to collapse
I have that now:
Code:
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
android.R.attr.state_pressed
}, context.getResources().getDrawable(android.R.drawable.list_selector_background));
but when I press on the TextView, the background goes orange
Auroratic said:
I have that now:
Code:
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
android.R.attr.state_pressed
}, context.getResources().getDrawable(android.R.drawable.list_selector_background));
but when I press on the TextView, the background goes orange
Click to expand...
Click to collapse
now your setting a state with a state "list" so not sure that will ever work.. needs to be a simple drawable