Hello
I made two applications. One for an activity receiving user input, the
other for a background service.
In the service package, I create two files
(startSensorBroadReceiver.java, remoteService.java).
StartSensorBroadReceiver will receiver an intent from activity
application and call method inside of the service.
There is no erron in the codes but I can not excute what is want.
I meet a message like this "The application ServiceExample has stopped
unexpectedly. Please try again."
could you let me know. what is wrong? and how I can call service
methods from broadcastReceiver?
/**
* startSensorBroadReceiver.java
*/
//broad cast receiver
public class StartSensorBroadReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String whichTaskStartService = intent.getStringExtra
("whichTaskStartService");
RemoteService s = new RemoteService();
if(whichTaskStartService==null){
Toast.makeText(context,R.string.error_unlocksensor_operation,
Toast.LENGTH_SHORT).show();
}
else if(whichTaskStartService.equals("StartSensor"))
{
//start service
Toast.makeText(context,"start sensor function call",
Toast.LENGTH_SHORT).show();
s.startSensor();
}
else if(whichTaskStartService.equals
("StopMotionDetectUnlockSensor"))
{
//stop service
Toast.makeText(context,"stop sensor function call",
Toast.LENGTH_SHORT).show();
RemoteService s = new RemoteService();
s.stopSensor();
}
}
}
/**
* remoteService.java
*/
public class RemoteService extends Service {
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this,R.string.service_start,
Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,R.string.service_destory,
Toast.LENGTH_SHORT).show();
}
SensorManager sensorManager;
public void startSensor(){
String service_name = Context.SENSOR_SERVICE;
sensorManager = (SensorManager)getSystemService(service_name);
sensorManager.registerListener(accelSensorListener,
SensorManager.SENSOR_ACCELEROMETER,SensorManager.SENSOR_DELAY_UI);
}
public void stopSensor(){
sensorManager.unregisterListener(accelSensorListener);
}
@SuppressWarnings("deprecation")
SensorListener accelSensorListener = new SensorListener() {
@Override
public void onAccuracyChanged(int arg0, int arg1) {
// TODO Auto-generated method stub
}
SensorDetectClass motion;
@Override
public void onSensorChanged(int sensor, float[] values) {
// TODO Auto-generated method stub
if(sensor == SensorManager.SENSOR_ACCELEROMETER)
{
};
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en