hi everybody!
over the course of 30 years, i've developed software on a lot of hardware platforms and operating systems. so i'm not a noob by any means to programming.
BUT, with that said, my last years of development were on the windows mobile platform. i jumped to the android about 8 months ago with a Droid X. it's been great, but the developer bug in me wants to create something for the droid...
i've spent 2-3 months reading about android development and working my way through several great development books ("Hello Android," The Pragmatic Programmers).
Do any of you have any examples of implementing a timer? i want to create a unique clock as a live wallpaper and i need to setup a 1 second timer that upon completion asynchronously informs another thread to begin processing. there is just so much development data on the web that i'm drowning in it!!!!
any help, pointers, and/or web-site references would be greatly appreciated.
thanks a lot,
marvin
Hi. I'm like you; I've been a C/C++ developer and worked with various hardware (mostly microcontrollers) for 20+ years. I've only recently gotten into java.
Here's a sample I wrote for someone a while back to demonstrate a timer. It isn't quite what you're looking for, but it should be adaptable to your situation.
It creates an activity, then uses a timer task to countdown from 10 to 0 and sends messages to a handler to updates the title of the app to reflect this.
Code:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class MyTestTimerActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Timer t=new Timer();
t.scheduleAtFixedRate(mTimerTask,1,1000);
}
Handler mHandler=new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case 0:
setTitle("hello_"+msg.arg1);
break;
case 1:
setTitle("timer_"+msg.arg1);
break;
}
super.handleMessage(msg);
}
};
protected TimerTask mTimerTask=new TimerTask(){
private int count=10;
private Message msg;
@Override
public void run(){
msg=mHandler.obtainMessage(0);
msg.arg1=count;
msg.sendToTarget();
count--;
if(count==-1){
cancel();
}
}
};
Thanks Gene. i'll study what you wrote and work it in as i can.
i spent 15 years writing asynchronous driver software on various VAX/VMS computers. it was so easy to do this task (sigh).
the problem i'm having with learning java, android SDK, and all is that is seems like i'm going around in circles reading about the various ways to do this task. and then i get confused with doing it on java or via xml.....whew. reminds me of the early days of my career.
thanks again.
Related
Hi,
all my lack of Java knowledge is causing pain...
I need to create a listener that takes some actions (including releasing the player instance) after a audio/video clip has finished to playback.
In the Android Dev Guide (description of MediaPlayer class) I found the following:
the player engine calls a user supplied callback method, OnCompletion.onCompletion(), if a OnCompletionListener is registered beforehand via setOnCompletionListener(OnCompletionListener). The invoke of the callback signals that the object is now in the PlaybackCompleted state.
Then in my code i have:
...
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
...
MediaPlayer mp = MediaPlayer.create(this, R.raw.clip);
// Here i get a message that onCompClip cannot be resolved
...
mp.setOnCompletionListener (onCompClip);
mp.start();
...
public void onCompClip (MediaPlayer my_mp) {
...
my_mp.release();
}
Anybody can please explain what i do wrong?
Also, do is it better to always release the MediaPlayer instance before playing another clip?
Thanks in advance,
x70
Hey guys, where can I find information about how to change one of my java programs such that it runs for android (eg MouseListener ---> onTouchListener) I know the the android website has info but can someone give me a direct link as to where it is because as we all know, its a huge site.
Basically, I have a ready program that I need to change(UI's etc..) so that it can work for android.
Ok this is a sample program. Can u guys gimme an editted version so i can base my real program off of it
import java.awt.event.*;
import javax.swing.*;
public class Box extends JApplet {
public void init() {
DisplayBox display = new DisplayBox();
setContentPane(display);
}
}
class DisplayBox extends JPanel {
DisplayBox() {
setBackground(Color.red);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.green);
g.fillRect (10,130, 80, 140);
g.setColor(Color.black);
g.drawString("Box", 15 , 85);
}
}
When you say Java program, do you mean written for a desktop OS or actually written for Android? Because I'm sure that while the Android SDK uses Java, many of the APIs are different, so it may not work properly if you did change it.
But then again, I have never attempted this before, so I may be wrong
I mean a program written to run the computer yes Like any basic program such as hello world.And i am using eclipse to run an android simulator so it will run with only with the android code and not just basic java.....
android doesn't have swing.
you are going to need to create the ui using xml.
res > layout > main.xml
there are tons of tutorial on how to make ui's for android all over the web.
http://developer.android.com/guide/topics/ui/index.html
Well you are not forced to create your layout using XML you could define it via Java too but the fact remains that you have to rebuild and redesign your user-interface and probably your user-interaction mechanism too.
Hello to all You developers out there.
I hope, that you can help me with understanding the Android Looper.
I have implemented a Service, which is intended to speak to a web Service and a database. This Service has a MainThread where it should do its work. I planned to communicate over Android Handler Objects. So I spend this Service two Handler: One for the Service and One for the Thread which is bound over the Android Looper
The MainThread Looks like this:
Sorry. The Forum seems to disallow me to post Source with the message: as a new user I'm not allowed to post links. But i don't have some in the initially post.
Anyway, let me describe my simple Thread.
It declares a handler which is instansiated in the overridden run Method between looper.prepare and looper.loop.
The it has a simple getHandler Method.
My question now is:
If I call the thread.start in the onCreate method of the Service, do the bloopers start infinitely looping like a while(true) statement? Or only if a message is passed through the handler which is my preferred behavior.
Do I have to use looped.quit to wait, because i don't found a activate method of the looper.
Am happy for any hint since the API for looper doesn't enlightened me
Code:
public class MainThread extends Thread{
private Handler looperHandler;
public MainThread(){
super();
}
@Override
public void run(){
Looper.prepare();
looperHandler = new Handler(){
@Override
public void handleMessage(Message message){
}
};
Looper.loop();
}
public Handler getHandler(){
return this.looperHandler;
}
public void stopThread(){
looperHandler.getLooper().quit();
}
}
Hmm I think if noone knows how this Android.Looper or the HandlerThread is working, how can my Service Communicate with my Activity without using IPC?
I need a simple CallBack from my Handler to my Activity.
The thing I try to do is, to get a Background Thread, which is not infinetly looping, but I can Activate it to do different work. After the work is finished my Service should be able to pass Message or Intent Objects.
I read the hole day Android API and know I'm completely lost.
please give me a hint.
it seems like I can create an Activity thus:
Code:
public class MainActivity extends Activity
and then create other activities like this:
Code:
public class SplashActivity extends MainActivity
and the SplashActivity should inherit the "extend Activity".
Fine.
But what is the point in this? It seems that even if I extend a master java file I still don't get any of the other imports inherited, like os.Bundle.
If I could inherit all the imports from a master java file I'd understand extending the MainActivity.
Can anyone shed light on this?
extends has absolutely nothing to do with imports. It allows the subclass to inherit public and protected methods and variables of the superclass and also allows you to override the methods if you choose to.
To fully understand the concept behind this, i suggest read a java book.. xD But without going into a whole lot of confusing stuff ill give it a go...
The example you gave you are essentially saying SplashActivity is a grandchild of Activity. the Activity is unique to the android platform. here is a basic java example:
Class Car extends Vehicle or Class truck extends Vehicle.. extends does not just give you methods, you inherit methods that override previous ones. if you are looking for a quick way to import packages like "EditText" after typing that use: Cntl + Shift +O.
extends is a java term for inheritance. you can think about it like if you had a Fruit class with certain functionality, then you have an Apple class that extends Fruit. Apple gains all the functionality from Fruit but you can then add new functionality specific to an Apple class.
and if you think this is done for import reasons then you are thinking on a whole different level than the makers of java. try getting an ide that auto imports Classes as you use them in your code, like IntelliJ
Thanks for the information guys.
I knew extends would allow method inheritance, I just though it would also allow package import inheritance.
One of the hurdles I come up against in learning Android development is that even the Google tutorials don't tell you that you need to import different packages for different things - e.g. the Tabs tutorial doesn't explain that you need to import all sorts of stuff and the only way I got it working was that Eclipse is smart and will suggest solutions to your errors.
Thanks again.
Subjective Effect said:
Thanks for the information guys.
I knew extends would allow method inheritance, I just though it would also allow package import inheritance.
One of the hurdles I come up against in learning Android development is that even the Google tutorials don't tell you that you need to import different packages for different things - e.g. the Tabs tutorial doesn't explain that you need to import all sorts of stuff and the only way I got it working was that Eclipse is smart and will suggest solutions to your errors.
Thanks again.
Click to expand...
Click to collapse
The import statement in java is kind of misleading. It's not really importing anything. It just allow you to refer to a class in that java file by using it's short class name (Activity) instead of it's fully qualified name (android.app.Activity). You never really have to use an import statement in your code but if you don't you just wind up having to type the full package name in front every time you use a class name.
So say for instance you didn't have an
import android.app.Activity;
statement in your java file. In order to get the compiler to know what Activity you are talking about you would need to write android.app.Activity everywhere in your code instead of the much simpler way of importing it once and referring to it as Activity.
Hope that made sense.
Also, say for example you had another class in a different package with the same name...
com.example.Activity
If you wanted to use that class in the same java file that you are using android.app.Activity an import is not even going to help you. You would always need to use the fully qualitied name to refer to each Activity class because if you just say "Activity" the compiler don't know if you mean android.app.Activity or com.example.Activity.
Can anyone guide me to a guide/tutorial or anything that complements or compares Android and JAVA programming? For example, the classes in Android that are not in JAVA, or how classes in JAVA (such as Scanner for the keyboard) are used/translated into Android. I'm trying to make a very simple app where a user will input numerical scores for 3 players (this is the money lost by the players), and then these numbers are added, and that becomes the score for the fourth player (this is the money gained by the player who won). Now, this would be quite simple with JAVA, but I have no idea how to do it in Android. I've done the "MyFirstApp" that Android has on its website, but that's more copy this code into Android Studio and done. If someone could link me to a guide that possibly references JAVA (the only programming language I know), I believe it'd be much easier to catch up with what is happening and how.
Thank you.
TextOnScreen said:
Can anyone guide me to a guide/tutorial or anything that complements or compares Android and JAVA programming? For example, the classes in Android that are not in JAVA, or how classes in JAVA (such as Scanner for the keyboard) are used/translated into Android. I'm trying to make a very simple app where a user will input numerical scores for 3 players (this is the money lost by the players), and then these numbers are added, and that becomes the score for the fourth player (this is the money gained by the player who won). Now, this would be quite simple with JAVA, but I have no idea how to do it in Android. I've done the "MyFirstApp" that Android has on its website, but that's more copy this code into Android Studio and done. If someone could link me to a guide that possibly references JAVA (the only programming language I know), I believe it'd be much easier to catch up with what is happening and how.
Thank you.
Click to expand...
Click to collapse
https://www.youtube.com/watch?v=QAbQgLGKd3Y
TheNewBoston has some good tutorials for beginners (personal experience)
For text input you can use EditText which extends the View class. They are all java classes. In the xml layout of your activity you add a new edittext:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/activity_main_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/activity_main_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GET"/>
</RelativeLayout>
the id is important. You will use it to "find" the view from the java code.
Then in the activity(java) you will use their methods to set and get the needed info:
activity_main.java
public class MainActivity extends ActionBarActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText) findViewById(R.id.activity_main_et);
Button btn = (Button) findViewById(R.id.activity_main_btn);
btn.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
// do whatever you like when the button is clicked
et.setText(et.getText() + " +"); // just some random stuff
}
});
}
}
Hope this helps. Good Luck
TextOnScreen said:
Can anyone guide me to a guide/tutorial or anything that complements or compares Android and JAVA programming? For example, the classes in Android that are not in JAVA, or how classes in JAVA (such as Scanner for the keyboard) are used/translated into Android. I'm trying to make a very simple app where a user will input numerical scores for 3 players (this is the money lost by the players), and then these numbers are added, and that becomes the score for the fourth player (this is the money gained by the player who won). Now, this would be quite simple with JAVA, but I have no idea how to do it in Android. I've done the "MyFirstApp" that Android has on its website, but that's more copy this code into Android Studio and done. If someone could link me to a guide that possibly references JAVA (the only programming language I know), I believe it'd be much easier to catch up with what is happening and how.
Thank you.
Click to expand...
Click to collapse
I started with watching this series of YouTube videos http://www.mybringback.com/series/android-basics/
AKA "MyBringBack" on YouTube channel.
Mind you I had no idea of what any kind of programming consisted of at the time, and have now had 5 apps published on Google Play (albeit, some pretty basic), all thanks to these videos and StackOverflow. I also believe this "Travis" in the video is the same as TheNewBoston. Either way, they are both GREAT starting points! Good luck, determination will get you where you want to be.
Happy Coding!