I’m trying to start an activity from a service running in background but its not working.. here’s the code. the service class uses an intent to call activity class. can anyone help me out??
//The service class
public class ServiceTemplate extends Service
{
// code to execute when the service is starting up
@Override
public void onStart(Intent intent, int startid)
{
Toast.makeText(getBaseContext(), "Accelerometer initiated", Toast.LENGTH_SHORT).show();
// to set a delay
Runnable mMyRunnable = new Runnable()
{
@Override
public void run()
{
Toast.makeText(getBaseContext(), "Accelerometer running", Toast.LENGTH_SHORT).show();
// Change state here
}
};
Handler myHandler = new Handler();
myHandler.postDelayed(mMyRunnable, 5000);
Intent i = new Intent(this, TimerAct.class);
startActivity(i);
}
}
//The Activity class
public class TimerAct extends Activity
{
static TextView timeDisplay;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
int length = 30000;
timeDisplay = (TextView) findViewById(R.id.timer);
timeDisplay.setText("Time left: " + length / 1000);
}
}
Since a
Serviceis not a UI component, you need to start a new task. Use FLAG_ACTIVITY_NEW_TASK: