Launching an Activity from a Service

Posted by nldev on Stack Overflow See other posts from Stack Overflow or by nldev
Published on 2011-03-31T08:02:17Z Indexed on 2012/10/26 5:02 UTC
Read the original article Hit count: 195

Filed under:

When I am trying to launch a call activity from a Service, I get a NullPointerException. Here is my code:

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + number));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);

I get the exception on the startActivity line.

I tried to use getApplication.startActivity and getApplicationContext.startActivity but no luck.

Any ideas?

edit : Maybe some usefull info: I am trying to create a service that will run on the background and scan sensor data, when a certain signal is given i would like to maken an automated call to a number.

edit : full adb error code:

03-31 09:04:10.214: ERROR/AndroidRuntime(1896): Uncaught handler: thread main exiting due to uncaught exception 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): java.lang.RuntimeException: Unable to instantiate service dfz.epilepsiedetector.services.DetectionService: java.lang.NullPointerException 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2668) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread.access$3100(ActivityThread.java:116) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1846) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.os.Handler.dispatchMessage(Handler.java:99) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.os.Looper.loop(Looper.java:123) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread.main(ActivityThread.java:4203) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at java.lang.reflect.Method.invokeNative(Native Method) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at java.lang.reflect.Method.invoke(Method.java:521) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at dalvik.system.NativeStart.main(Native Method) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): Caused by: java.lang.NullPointerException 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.content.ComponentName.<init>(ComponentName.java:75) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.content.Intent.<init>(Intent.java:2302) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at dfz.epilepsiedetector.services.DetectionService.<init>(DetectionService.java:35) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at java.lang.Class.newInstanceImpl(Native Method) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at java.lang.Class.newInstance(Class.java:1472) 03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2665)

edit Trimmed class code:

`public class DetectionService extends IntentService implements SensorEventListener {
private SensorManager mSensorManager; private Sensor mAccelerometer; private boolean hasSeizure = false;

private final int POLLS_PER_SECOND = 10;

public DetectionService() { super("EpilepsionDetectionService");
//mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); // mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

/*Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:" + "+31648363944")); callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);*/

Intent callIntent = new Intent(DetectionService.this, InformationActivity.class); callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(callIntent);

//((Activity) getContext()).startActivity(callIntent); }`

© Stack Overflow or respective owner

Related posts about android