Hello Guys,
i have an issue with my "onSensorChanged" event. I write a test app to acces my accelator sensor on the "Samsung Galaxy S5 Mini". This is my code:
My MainActivity implements the SensorEventListener:
Code:
public class MainActivity extends Activity implements SensorEventListener {
And i implement onSensorChanged and onAccuracyChanged as well:
Code:
@Override
public void onSensorChanged(SensorEvent event) {
Log.d("Test", "SensorChange"); //never called
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float[] vals = event.values.clone();
tvX.setText("X: " + Float.toString(vals[0]));
tvY.setText("Y: " + Float.toString(vals[1]));
tvZ.setText("Z: " + Float.toString(vals[2]));
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
Not even "Log.d("Test", "SensorChange");" is called a single time.
I am working with Android Studio 0.8.14 at the moment. I use an older version, cause I got some trouble to import a 0.8.14 project to the version 1.0. The wierd thing is, that I create a simillar app with eclipse some days ago and got no issues to read the accelator sensor. I would be glad if, someone knows how to solve this problem.
I solved the problem. I just forgot to register the listener:
Code:
sensorManager.registerListener(this, sensor , SensorManager.SENSOR_DELAY_NORMAL);
Related
As described in manual, to use custom class in .aidl files, it must to implement Parcelable interface :
Code:
package com.shadower;
import android.os.Parcel;
import android.os.Parcelable;
public class Params implements Parcelable{
private long timeout;
public static final Parcelable.Creator<Params> CREATOR = new Parcelable.Creator<Params>() {
@Override
public Params createFromParcel(Parcel in) {
return new Params(in);
}
@Override
public Params[] newArray(int size) {
return new Params[size];
}
};
public Params(Parcel in) {
timeout = in.readLong();
}
public void writeToParcel(Parcel out) {
out.writeLong(timeout);
}
public void setTimeOut(long timeout) {
this.timeout = timeout;
}
public long getTimeout() {
return timeout;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeLong(timeout);
}
}
then must be imported into .aidl:
Code:
package com.shadower;
import com.shadower.IShadowerServiceCallback;
import com.shadower.Params;
....
but **** happens :
Code:
couldn't find import for class com.shadower.Params IShadowerService.aidl /shadower/src/com/shadower line 5 Android AIDL Problem
wtf is going on?
I have 2 classes in one .java file and it runs fine without errors or anything (the second class is used as a timer and changes variables every second) everything works but it wont call methods properly. Any idea of why this would be??? Heres my code of the second class.
Code:
class MyTime extends TimerTask{
//java.text.DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public game timecall2= new game();
public MyTime(Context ctx) {
// create internal instance
Context ctx2;
}
@Override
public void run() {
game.sec--;
if(game.sec==-1){game.sec=59;game.min--;}
game.Title2(); // set text in title bar
}
}
It would be easier if you explain more cleary, what you want to do.
Or post more code.
public game timecall2= new game();
I think, game is your first class?
Then you want to use the variable sec of the class game?
-> are they declared as static or why you don't call it over the object timecall2 you created?
Sry, but without more Code/Information to unterstand your problem, it's difficult to help. Also don't know, how skilled your are in programming.
*game* is the first class
I've tried calling the method through *timecall2* object but it doesn't help.
I've tried declaring the function as both static and no-static but nothing seems to change.
I've got lots of programming experience in other languages, but I'm less experienced with Java
And what's going wrong now?
Why don't you debug trough the code. Should be the easiest ways to find the problem.
You generally should use getters and setters for accessing variables inside of another class. Meaning that you have a method to set the value of the variable and a method to retrieve the value
Code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MyClass extends Activity {
private static int mMin = 0;
private static int mSec = 0;
private TextView titleText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView titleText = (TextView) findViewById(R.id.titleText);
}
public static int getSec() {
return mSec;
}
public static void setSec(int newVal) {
mSec = newVal;
}
public static int getMin() {
return mMin;
}
public static void setMin(int newVal) {
mMin = newVal;
}
public static void setTitleText(String newVal) {
if (titleText != null) {
titleText.setText(newVal);
}
}
}
Notice the "static" modifier of the class methods.
Generally, you wouldn't instantiate this activity class. Especially if it already lives in memory. What I am guessing that you are doing here is that "game" is your activity and that MyTime is an object that you are using from inside of the activity and that you need to modify variables that live in the main Activity.
If that is the case then you would just do something like this:
Code:
import java.util.TimerTask;
import com.myapp.game.MyActivity;
class MyTime extends TimerTask {
private int sec = 0;
private int min = 0;
@Override
public void run() {
sec = MyActivity.getSec();
min = MyActivity.getMin();
sec--;
if(sec==-1) {
sec=59;
min--;
}
String title = String.valueOf(min)+":"+String.valueOf(sec); //obviously you need formatting
MyActivity.SetTitle(title); // set text in title bar
MyActivity.setSec(sec);
MyActivity.setMin(min);
}
}
That said... I just did the above as an example. This is entirely the wrong way to make a game timer. There is a better example here
From what I remember from C++, you would have a "base" class and then other classes beneath that base class.
To use the base class methods, the other classes had to be "friends". I'm a little rusty on my JAVA syntax...is "extends" the same thing as a friend class?
Java doesn't have friend functions, and "extends" means that the class is a subclass of whatever its extending.
To access a function/variable between classes that said function/variable must be static. Beware tho when do this depending on you implementation you must check for null variables. Since its static you can access you dont need a class object to access them through.
ak. SomeClass.function();
instead of using SomClass sc = new SomeClass(); sc.function();
since u can access it at any time its contents may not be initialized and be null. So ALWAYS check for null varaibles! lol. That or u can have one variable of that class to check if its class is initialized. such as..
SomeFunction.isInit();
where isInit(); is
private static initialized = false;
public static boolean isInit()
{
return initialized;
}
where in your onCreate & on onDestroy functions you set the initialized variable accordingly.
or..u could just do if(SomeClass.this!=null) lol :S
/me stops writting wot
Thanks for the input everyone. I've realised the problem (but still can't fix it). I can call methods in other classes from my timer class.... but my main class that has the methods that I need implements OnClickListener (public class game extends Activity implements OnClickListener) so it is ONLY updating the view methods when something is clicked on. How should I go about fixing it so that I can call methods that will update when a timer calls them (e.g. I display the remaining time in the title bar and it doesn't update the current time UNLESS a button is clicked on)
Why not run the timer as a thread in the activity class itself?
I'd like to register a listener inside AccessibilityService extented class but I don't know how to achieve that as I don't have a instance of it (it is started using intent). Or maybe there is another way of getting callback from this class? I just want to notify another class when the "onAccessibilityEven()" is trigerred.
Code:
public class NotifyService extends AccessibilityService {
// declaration of the interface
public interface Listener {
public void onNotifyChange(boolean newNotification);
}
// registration of the listener
public void registerListener(Listener listener) {
mListener = listener;
}
// ...
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if(mListener != null) {
mListener.onNotifyChange(true);
}
}
}
Can you build an handler in the other class? If yes, just send a message and the handler in the other class will receive it If you need an example I will write it.
Do you mean the same handler which is usually used for communicating between threads? If you could give me some example would be great.
Hello, I could use some guidance with my first app. I have a digi wifi module + sam3x8e arm cortex m3 microcontroller connected tethered to my phone and I would like to send some data between two. Microcontroller is pulling data from a pressure sensor, air fuel ratio sensor, flow sensor and and transmits data as: B25A13F1B25A13F1B25A13F1 where B=boost, A= AFR, F=flow
In app I need to pull the info of the tethered devices, send info to the microcontroller. Then I need to process incoming string and display it in app.
Since this is my first app, I could use help on how to structure the app itself and links to the reference materials you guys are using. I have read bunch of tutorial so far and created a simple TCP socket app that can transmit data when i press the button but thats not what i need to do, and I do not understand how to make the app continiously process incoming data.
Thank you,
Yaro
So I ended up with 2 fragments inside my main activity, interface, and a service that I can bind to.
I really need your help guys, Java aint that tough but I very much lack understanding of philosophy behind app design. Where should I break down incoming String data B25A13F1B25A13F1B25A13F1 into useable data. Should I do in service or Fragment? Please help... it took me way too long to figure out that I can't start service from fragment
Code to illustrate the communication.
Communication from service to GUI (main activity):
Code:
private class IncomingMessageHandler extends Handler {
[user=439709]@override[/user]
public void handleMessage(Message msg) {
switch (msg.what) {
case MyService.MSG_SET_INT_VALUE:
textIntValue.setText("Int Message: " + msg.arg1);
break;
case MyService.MSG_SET_STRING_VALUE:
String str1 = msg.getData().getString("str1");
textStrValue.setText("Str Message: " + str1);
break;
default:
super.handleMessage(msg);
}
}
}
Communication from Service to GUI (Service)
Code:
private void sendMessageToUI(int intvaluetosend) {
Iterator<Messenger> messengerIterator = mClients.iterator();
while(messengerIterator.hasNext()) {
Messenger messenger = messengerIterator.next();
try {
// Send data as an Integer
messenger.send(Message.obtain(null, MSG_SET_INT_VALUE, intvaluetosend, 0));
// Send data as a String
Bundle bundle = new Bundle();
bundle.putString("str1", "ab" + intvaluetosend + "cd");
Message msg = Message.obtain(null, MSG_SET_STRING_VALUE);
msg.setData(bundle);
messenger.send(msg);
} catch (RemoteException e) {
// The client is dead. Remove it from the list.
mClients.remove(messenger);
}
}
}
You CAN start a Service from a Fragment:
Code:
getActivity().startService(<your parameters here>);
Everything related to this (e.g. the bind action) can be done from a Fragment if you put getActivity() in front of it.
nikwen said:
You CAN start a Service from a Fragment:
Code:
getActivity().startService(<your parameters here>);
Everything related to this (e.g. the bind action) can be done from a Fragment if you put getActivity() in front of it.
Click to expand...
Click to collapse
Thank you sir, that peace of information simplified my life quite nicely.
I found very nice simple example that works on the net and it works. kinda... After I click my button there is about 2-3 second delay before my client displays the response. Also I am trying to send "Hello there" and I get "....t..Hello there". I am intending to communicate both ways, from the phone and to the phone at the same time and such a huge delay is unacceptable. Any ideas why? Code below.
Code:
package com.example.mytcpcleint;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView serverMessage;
Thread m_objThreadClient;
Socket clientSocket;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serverMessage=(TextView)findViewById(R.id.textView1);
}
public void Start(View view)
{
m_objThreadClient=new Thread(new Runnable() {
public void run()
{
try
{
clientSocket= new Socket("192.168.43.59",9750);
ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
oos.writeObject("Hello there");
// Message serverMessage= Message.obtain();
// ObjectInputStream ois =new ObjectInputStream(clientSocket.getInputStream());
// String strMessage = (String)ois.readObject();
// serverMessage.obj=strMessage;
// mHandler.sendMessage(serverMessage);
oos.close();
// ois.close();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
m_objThreadClient.start();
}
Handler mHandler = new Handler() {
[user=439709]@override[/user]
public void handleMessage(Message msg) {
messageDisplay(msg.obj.toString());
}
};
public void messageDisplay(String servermessage)
{
serverMessage.setText(""+servermessage);
}
}
Hello, I'm extremely new to both android application developing, and this forum.
I am trying to open another screen ( activity ) when a button ( readyButton ) on the splash screen is pressed. I've tried at least ten different times with different tutorials to no avail, this is my current code which didn't work, and instead forces the app to crash.
Code:
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
Please help.
The button's name is 'readybutton'
the second activity name is 'SecondActivity'
also, where am I supposed to put this code into the java class? Here is how it is currently set up:
Code:
package com.unchat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
// Default Items
public class FirstActivity extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
/** New button code start */
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
/** new button code end */
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
[user=439709]@override[/user]
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
// End of Default Items
incorrectly announces intent
Try like this.
Code:
Intent intent = new Intent(this, SecondActivity.class) ;
startActivity(intent);
and check whether your Listener
1. you need to use the full name of your activity, including the package name.
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
rhmsoft said:
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
Click to expand...
Click to collapse
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
bornander said:
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
Click to expand...
Click to collapse
I thought you had to declare all your activities in the manifest?
Log
Post the error log please