Android 另一进程中的活动启动后是否回调?

Android 另一进程中的活动启动后是否回调?,android,android-activity,callback,start-activity,Android,Android Activity,Callback,Start Activity,如果我对另一个进程中的某个活动执行startActivity();在新活动真正启动的起点(从后台服务启动新活动)接收回调是否有一种好方法?我的意思是我可以做一个广播,但那似乎很差劲。有更好的方法吗?如果在同一个或另一个进程中对活动执行startActivity()(并假设第二个活动启动),调用活动将进入第一个暂停状态,然后进入停止状态。这意味着不会处理任何回调。但是,您可以调用startActivityForResult()而不是startActivity(),并接收onActivityResu

如果我对另一个进程中的某个活动执行startActivity();在新活动真正启动的起点(从后台服务启动新活动)接收回调是否有一种好方法?我的意思是我可以做一个广播,但那似乎很差劲。有更好的方法吗?

如果在同一个或另一个进程中对活动执行startActivity()(并假设第二个活动启动),调用活动将进入第一个暂停状态,然后进入停止状态。这意味着不会处理任何回调。但是,您可以调用startActivityForResult()而不是startActivity(),并接收onActivityResult()回调。

如果您对同一进程或另一进程中的某个活动执行startActivity()(并假设第二个活动启动),则调用活动将进入先暂停,然后停止的状态。这意味着不会处理任何回调。但是,您可以调用startActivityForResult()而不是startActivity(),并接收onActivityResult()回调。

添加一个类MyApplication,该类扩展应用程序并在清单中提及,然后在其中放入两个布尔变量

      private static boolean activityVisible;

  public static void activityResumed() {
    activityVisible = true;
}

public static void activityPaused() {
    activityVisible = false;
}
 public static boolean isActivityVisible() {
    return activityVisible;
}
现在,当您在当前活动的onPause()和onStop()上执行startActivity()时,可以使用call activityPaused(),当您返回到同一个活动时,可以在重写的onResume()方法中调用activityResumed()


现在,通过使用MyApplication.isActivityVisible(),您可以了解您的活动是正在运行还是已暂停。

添加一个类MyApplication,该类扩展应用程序并在清单中提及,然后在其中放入两个布尔变量

      private static boolean activityVisible;

  public static void activityResumed() {
    activityVisible = true;
}

public static void activityPaused() {
    activityVisible = false;
}
 public static boolean isActivityVisible() {
    return activityVisible;
}
现在,当您在当前活动的onPause()和onStop()上执行startActivity()时,可以使用call activityPaused(),当您返回到同一个活动时,可以在重写的onResume()方法中调用activityResumed()


现在,通过使用MyApplication.isActivityVisible(),您可以了解您的活动是正在运行还是已暂停。

基本上,您所询问的是ipc。谷歌搜索更多信息

要实现这一点,您需要创建一个服务类并将这两个活动绑定到它

示例服务类如下所示

public class MyService extends Service{
    //create a handler that will be used to handle messages
    //this is just an example. Use static handlers
    final Handler handler=new Handler(){
         public void handleMessage(Message msg){
            //I've just created a static feild.Check below.
            MyFirstActivity.activity.seconActivityStarted();
         }
    }

    //create a Messenger object
    Messenger messenger=new Messenger(handler);

    @Override 
    public IBinder onBind(Intent intent){
       return messenger.getBinder()
    }
}
现在事情很简单

现在您必须将第一个活动与服务绑定

public class MyFirstActivity extends AppCompatActivity{
    //for the time being I'll just create a static field that will be used.
    //you can use an interface
    static MyFirstActivity activity;
    //create a ServiceConnection
    ServiceConnection connection=new ServiceConnection(/*contents of the service connection */);

   public void onStart(){
       super.onStart();
       activity=this;
       bindService(new Intent(this,MyService.class),connection,Context.BIND_AUTO_CREATE));
   }

   //following method will be called by the handler in the service
   public void secondActivityStarted(){
       //some code
   }

   //you have to handle the activity lifecycle with the bound service.
   //right now its described here

}
现在是第二项活动

public class SecondActivity extends AppCompatActivity{
    //create a ServiceConnection
    ServiceCOnnection serviceConnection=new ServiceConnection(){
         //call the handler at onServiceConnected
         public void onServiceCOnnected(ComponentName name, IBinder service){
              Messenger messenger=new Messenger(service);
              //compose a Message object as you like
              messenger.send(new Message());
         }
    };

    //bind this activity to the same service
    public void onStart(){
       super.onStart();
       bindService(new Intent(this,com.package.name.MySerice.class),serviceConnection,Context.BIND_AUTO_CREATE);
    }
}
就这样。根据要求对其进行修改


上面提到的代码只是工作结构。

基本上你要问的是ipc。谷歌搜索更多信息

要实现这一点,您需要创建一个服务类并将这两个活动绑定到它

示例服务类如下所示

public class MyService extends Service{
    //create a handler that will be used to handle messages
    //this is just an example. Use static handlers
    final Handler handler=new Handler(){
         public void handleMessage(Message msg){
            //I've just created a static feild.Check below.
            MyFirstActivity.activity.seconActivityStarted();
         }
    }

    //create a Messenger object
    Messenger messenger=new Messenger(handler);

    @Override 
    public IBinder onBind(Intent intent){
       return messenger.getBinder()
    }
}
现在事情很简单

现在您必须将第一个活动与服务绑定

public class MyFirstActivity extends AppCompatActivity{
    //for the time being I'll just create a static field that will be used.
    //you can use an interface
    static MyFirstActivity activity;
    //create a ServiceConnection
    ServiceConnection connection=new ServiceConnection(/*contents of the service connection */);

   public void onStart(){
       super.onStart();
       activity=this;
       bindService(new Intent(this,MyService.class),connection,Context.BIND_AUTO_CREATE));
   }

   //following method will be called by the handler in the service
   public void secondActivityStarted(){
       //some code
   }

   //you have to handle the activity lifecycle with the bound service.
   //right now its described here

}
现在是第二项活动

public class SecondActivity extends AppCompatActivity{
    //create a ServiceConnection
    ServiceCOnnection serviceConnection=new ServiceConnection(){
         //call the handler at onServiceConnected
         public void onServiceCOnnected(ComponentName name, IBinder service){
              Messenger messenger=new Messenger(service);
              //compose a Message object as you like
              messenger.send(new Message());
         }
    };

    //bind this activity to the same service
    public void onStart(){
       super.onStart();
       bindService(new Intent(this,com.package.name.MySerice.class),serviceConnection,Context.BIND_AUTO_CREATE);
    }
}
就这样。根据要求对其进行修改



上面提到的代码只是一个工作结构。

那么界面呢?你是什么意思?这是另一个过程;我该怎么做呢?方法就是提供服务。当第一个活动开始时,将其与服务绑定。在服务中,创建一个处理程序(您将在创建Messenger对象时使用的处理程序),该处理程序包含对其handleMessage()中第一个活动的接口调用。在新线程中启动的新活动上,使用相同的服务绑定,并使用Messenger对象调用处理程序。因此,通信将通过处理程序进行,数据将在消息对象中传输。如果你没有得到答案,在这里回答。回答得很好。请把它作为答案,这样我就可以接受它作为正式的答案。界面呢?你是什么意思?这是另一个过程;我该怎么做呢?方法就是提供服务。当第一个活动开始时,将其与服务绑定。在服务中,创建一个处理程序(您将在创建Messenger对象时使用的处理程序),该处理程序包含对其handleMessage()中第一个活动的接口调用。在新线程中启动的新活动上,使用相同的服务绑定,并使用Messenger对象调用处理程序。因此,通信将通过处理程序进行,数据将在消息对象中传输。如果你没有得到答案,在这里回答。回答得很好。请把它作为答案,这样我就可以接受它作为正式的答案。是的,但是我必须停止开始的活动,对吗?我希望它继续运行。根据Android设计,只有前台活动是正在运行的活动,后台活动会根据系统资源需求自动推送到停止状态,甚至被销毁。我认为你不能让第一个活动继续运行好吧,我错了。该活动实际上是从后台服务启动的。由于您需要进行进程间通信,您可以使用Messenger为该服务创建接口。是的,但我必须停止启动的活动,对吗?我希望它继续运行。根据Android设计,只有前台活动是正在运行的活动,后台活动会根据系统资源需求自动推送到停止状态,甚至被销毁。我认为你不能让第一个活动继续运行好吧,我错了。该活动实际上是从后台服务启动的。由于您需要进行进程间通信,因此可以使用Messenger为该服务创建接口