How can we name an activity through a service in android?

I want to know if it is possible to call a function through a background service in Android, for example:

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 name my activities ......

+3
source share
2 answers

You can call the operation while onStart () of your service .....

A fragment may 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);
...

}
+5
source

I believe that launching interactive-interactive activity from a non-interactive service runs counter to the design of Android, as it pulls control out of the user's control.

Notifications are a mechanism designed to attract the attention of users from a background application and enable them to start interactive activity.

+4
source

Source: https://habr.com/ru/post/1739121/


All Articles