onTouchEvent doubt.. - Java for Android App Development

I have a doubt.
I read this example where the ontouchevent is been created in the class that extends activity.
http://www.jpct.net/wiki/index.php/Hello_World_for_Android
But is not ontouchevevent method of View's class ?
Depends from Activity ? Can you explain me how does it work ?

Related

problem in BroadcastReceiver App

hi,
i made an application to read sms.
my problem is to update a textview named txtMessaggio
from the class-reader.
the class is like "SmsReader extends BroadcastReceiver"
now i read the message and trow it on display via Toast.
ho i can put in txtMessaggio?
thanks

[Q] understanding problem with Android Looper

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.

[Q] transferring Windows mobile app to android

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.

Explain extends Activity to me because

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.

Java Print Stream

Well i am in the learning phase of java. I am unable to understand one thing .
As far as i have understood, methods are called upon on the objects. In "System.out.println()" 'out' is a field of type PrintStream in the class System. Being said that , i am unable to understand how come a method can be called using a field of a class ?
would really appreciate if someone clarify this concept.
in addition, below is the extract from the book "Java the complete reference", which states System.out and System.err are objects of class Printstream.
QUOTE:
System.in is an object of type InputStream; System.out and System.err are objects of
type PrintStream.
UNQUOTE:
Apologies if i have posted this in wrong forum.
Field is just a place where you hold an object's state, so there is no problem with calling method on field - method will be called on object.
If field of a class is public, it is the same situation as with any other variable holding an object: you can call method on any variable that refers to an object if you only have access to this variable.
What may be confusing is that out is static variable. It means that you don't need an object of System class to use static variable. If this method would not be static, you will have to write code like this:
Code:
System system = new System();
system.out.println(...);
The fact that field out is public may be also confusing for beginners, because usually fields are private - but with public members using System class is easier.
klisiecki said:
Field is just a place where you hold an object's state, so there is no problem with calling method on field - method will be called on object.
If field of a class is public, it is the same situation as with any other variable holding an object: you can call method on any variable that refers to an object if you only have access to this variable.
What may be confusing is that out is static variable. It means that you don't need an object of System class to use static variable. If this method would not be static, you will have to write code like this:
Code:
System system = new System();
system.out.println(...);
The fact that field out is public may be also confusing for beginners, because usually fields are private - but with public members using System class is easier.
Click to expand...
Click to collapse
one thing more , complete declaration of 'out' in class System is
" public final static PrintStream out = null;"
out is a reference variable of type PrintStream and is assigned a null reference.
Why don't we receive a "NullPointerException" Error when we call System.out.println?

Categories

Resources