access sensormanager

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

access sensormanager

Postby swordi » Tue Jun 02, 2009 10:24 am

hello

i try to access the sensormanager, but my application ends in a black screen with a "Activity... is not responding"

this are my lines of code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. sManager = (SensorManager)getSystemService( Context.SENSOR_SERVICE );
  3.  
  4. sManager.registerListener(sensorListener, sManager, SensorManager.SENSOR_ACCELEROMETER);
  5.  
  6.  
  7.  
  8. private SensorEventListener sensorListener = new SensorEventListener() {
  9.  
  10.    
  11.  
  12.     public void onAccuracyChanged(Sensor sensor, int accuracy) {
  13.  
  14.     }
  15.  
  16.  
  17.  
  18.     public void onSensorChanged(SensorEvent e) {
  19.  
  20.       synchronized (this) {
  21.  
  22.        
  23.  
  24.                 System.out.println("shaked");
  25.  
  26.        
  27.  
  28.       }      
  29.  
  30.     }
  31.  
  32.   };
  33.  
  34.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


i cant find any tutorial for the 1.5 version of the sensormanager - have you any links or hints for me??

thanks a lot
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Top

Postby metter » Tue Jun 02, 2009 12:40 pm

Hey.

Try this. Have fun :)

I have no idea what to make of the values it delivers though.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. public class MainActivity extends Activity {
  5.  
  6.     /** Called when the activity is first created. */
  7.  
  8.     @Override
  9.  
  10.     public void onCreate(Bundle savedInstanceState) {
  11.  
  12.         super.onCreate(savedInstanceState);
  13.  
  14.         setContentView(R.layout.main);
  15.  
  16.        
  17.  
  18.         SensorManager sManager = (SensorManager)getSystemService( Context.SENSOR_SERVICE );
  19.  
  20.        
  21.  
  22.        
  23.  
  24.         List<Sensor> liste = sManager.getSensorList(Sensor.TYPE_ALL);
  25.  
  26.         for(int a=0;a<liste.size();a++) {
  27.  
  28.                 System.out.println("Sensor "+a+": "+liste.get(a).getName());
  29.  
  30.         }
  31.  
  32.         sManager.registerListener(sensorEventListener, liste.get(0), sManager.SENSOR_DELAY_NORMAL);
  33.  
  34.        
  35.  
  36.        
  37.  
  38.     }
  39.  
  40.    
  41.  
  42.  
  43.  
  44.     private SensorEventListener sensorEventListener = new SensorEventListener() {
  45.  
  46.        
  47.  
  48.         public void onAccuracyChanged(Sensor sensor, int accuracy) {
  49.  
  50.         }
  51.  
  52.  
  53.  
  54.         public void onSensorChanged(SensorEvent e) {
  55.  
  56.           synchronized (this) {
  57.  
  58.                  
  59.  
  60.               System.out.println("Sensor event: "+e.values[0]+" "+e.values[1]+" "+e.values[2]+" ");
  61.  
  62.            
  63.  
  64.           }      
  65.  
  66.         }
  67.  
  68.  
  69.  
  70.                
  71.  
  72.       };
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
metter
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Jan 06, 2009 2:00 pm

Postby swordi » Tue Jun 02, 2009 12:41 pm

thanks man. i will try it today

is this working for you with 1.5 in the emulator???
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Postby noya » Tue Jun 09, 2009 10:48 am

Are you using the Openintents SensorSimulator? Because it supports only SDK 1.1 ...
User avatar
noya
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri May 08, 2009 3:16 pm
Location: Bergamo, Italy

Postby swordi » Tue Jun 09, 2009 10:51 am

no i dont use this thing

i get errors in the emulator when i use the sensormanager
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Postby jove » Tue Jun 09, 2009 8:02 pm

I've been having the same problem, 1.1 works but not 1.5 on the emulator

The SDK release notes says this:

http://developer.android.com/sdk/RELEASENOTES.html

Known Issues
Sensor problems in Emulator

If your application uses the Sensor API and you are running it in the emulator on the Android 1.5 system image, you may experience problems. Your application may generate ANR messages or crash when using the sensors. The problem is being investigated.
jove
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Apr 29, 2009 7:36 pm

Top

Postby swordi » Tue Jun 09, 2009 8:04 pm

ok thanks for your info

i stopped using sensormanager for now. maybe it will work in the new release ;)
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Postby noya » Thu Jun 11, 2009 10:26 am

Hi man!

I've just created this test application for accelerometer and it works... check it ! It just take accelerometer informations and print on screen. (uses SDK 1.5)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.android.sample;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.Context;
  8.  
  9. import android.content.Intent;
  10.  
  11. import android.hardware.SensorEvent;
  12.  
  13. import android.hardware.SensorEventListener;
  14.  
  15. import android.hardware.SensorManager;
  16.  
  17. import android.hardware.Sensor;
  18.  
  19. import android.os.Bundle;
  20.  
  21.  
  22.  
  23. import android.widget.TextView;
  24.  
  25.  
  26.  
  27.  
  28.  
  29. public class HelloAccelerometer extends Activity{
  30.  
  31.  
  32.  
  33.      private TextView mTxtView;
  34.  
  35.      
  36.  
  37.      private SensorManager mSensorManager;
  38.  
  39.    
  40.  
  41.      @Override
  42.  
  43.      protected void onCreate(Bundle savedInstanceState) {
  44.  
  45.           // TODO Auto-generated method stub
  46.  
  47.           super.onCreate(savedInstanceState);
  48.  
  49.  
  50.  
  51.           mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  52.  
  53.           mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  54.  
  55. SensorManager.SENSOR_DELAY_FASTEST);
  56.  
  57.           mTxtView = new TextView(this);
  58.  
  59.           setContentView(mTxtView);
  60.  
  61.      }
  62.  
  63.      
  64.  
  65.      public void updateTV(float p_x, float p_y, float p_z)
  66.  
  67.      {
  68.  
  69.           mTxtView.setText("x: "+p_x+", y: "+p_y+", z: "+p_z);
  70.  
  71.      }
  72.  
  73.      
  74.  
  75.      private final SensorEventListener mSensorListener = new SensorEventListener() {
  76.  
  77.          
  78.  
  79.           public void onSensorChanged(SensorEvent se)
  80.  
  81.           {
  82.  
  83.               float x = se.values[0];
  84.  
  85.               float y = se.values[1];
  86.  
  87.               float z = se.values[2];
  88.  
  89.                   updateTV(x, y , z);
  90.  
  91.           }
  92.  
  93.          
  94.  
  95.           public void onAccuracyChanged(Sensor sensor, int accuracy) {}
  96.  
  97.      };
  98.  
  99.      
  100.  
  101.      @Override
  102.  
  103.      protected void onResume()
  104.  
  105.      {
  106.  
  107.           super.onResume();
  108.  
  109.           mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
  110.  
  111.      }
  112.  
  113.      
  114.  
  115.      @Override
  116.  
  117.      protected void onStop()
  118.  
  119.      {
  120.  
  121.           mSensorManager.unregisterListener(mSensorListener);
  122.  
  123.           super.onStop();
  124.  
  125.      }
  126.  
  127. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
Last edited by noya on Thu Jun 11, 2009 10:32 am, edited 2 times in total.
User avatar
noya
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri May 08, 2009 3:16 pm
Location: Bergamo, Italy

Postby noya » Thu Jun 11, 2009 10:28 am

and off course... to debug you you have to use the Log.d function, maybe the error is just because you are using System.out function which is not to be used for debugging!
User avatar
noya
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri May 08, 2009 3:16 pm
Location: Bergamo, Italy

Postby swordi » Thu Jun 11, 2009 10:51 am

ok thanks man

i will try your source code in the next days. im very busy at the moment with a new small app

thanks

---
http://swordiApps.blogspot.com
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests