tags:

views:

215

answers:

2

I want to know if it is possible to call an activity through background service in android like :

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;



public class background extends Service{

 private int timer1;
 @Override
 public void onCreate() {
  // TODO Auto-generated method stub
  super.onCreate();

  SharedPreferences preferences = getSharedPreferences("SaveTime", MODE_PRIVATE);
  timer1 = preferences.getInt("time", 0);
  startservice();
 }

 @Override
 public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
 }

 private void startservice() {

  Handler handler = new Handler();
  handler.postDelayed(new Runnable(){
   public void run() {
    mediaPlayerPlay.sendEmptyMessage(0);
   }
  }, timer1*60*1000);
 }

 private Handler mediaPlayerPlay = new Handler(){

  @Override
  public void handleMessage(Message msg) {
   try
   {
    getApplication();
    MediaPlayer mp = new MediaPlayer();

    mp = MediaPlayer.create(background.this, R.raw.alarm);
    mp.start();

   }
   catch(Exception e)
   {
    e.printStackTrace();
   }
   super.handleMessage(msg);
  }
 };


 /*
  * (non-Javadoc)
  * 
  * @see android.app.Service#onDestroy()
  */
 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub

  super.onDestroy();
 }
}

i want to call my activity......

+1  A: 

I believe launching user-interactive Activity from a non-interactive Service goes against the design of Android, in that it would pull out control from under the user.

Notifications are the mechanism intended to get user's attention from a background app, and give them an opportunity to launch the interactive Activity.

Jim Blackler
Hi Jim, thanks for reply . sorry friend, please clear this i didn't get what you want to say..
Shalini Singh
+1  A: 

You can call an Activity while onStart() of your service.....

Snippet might be as follows:

@Override 
public void onStart(Intent intent, int startId)  { 
...
Log.i("Service", "onStart() is called"); 
Intent callIntent = new Intent(Intent.ACTION_CALL); 
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
callIntent.setClass(<Set your package name and class name here>);
startActivity(callIntent);
...

}
Vishwanath
Hey Thanks!!! Vishwanath.... it's working
Shalini Singh
Goood!!! Start Shalini
OM The Eternity