回调一旦Activity在另一个进程已经开始?
Callback once Activity in another process has started?
如果我在另一个进程的 Activity 上启动 Activity() ;新Activity真正启动的起点(启动新Activity形成后台服务)有没有好的方法接收回调?我的意思是我可以进行广播,但这似乎很蹩脚。有更好的方法吗?
如果您在同一个或另一个进程中的 Activity 上执行 startActivity()(并假设第二个 activity 开始),调用 Activity 将继续进入先 PAUSED,然后 STOPPED 状态。这意味着不会处理回调。但是,您可以调用 startActivityForResult() 而不是 startActivity() 并接收 onActivityResult() 回调。
添加一个 class MyApplication 扩展应用程序并在您的清单中提及,然后在其中放置两个布尔变量。
private static boolean activityVisible;
public static void activityResumed() {
activityVisible = true;
}
public static void activityPaused() {
activityVisible = false;
}
public static boolean isActivityVisible() {
return activityVisible;
}
所以现在您可以在当前 activity 的 onPause() 和 onStop() 上执行 startActivity() 时调用 activityPaused(),以及当您return 回到原来的状态 activity 在覆盖的 onResume() 方法中调用 activityResumed()。
现在,通过使用 MyApplication.isActivityVisible(),您可以了解您的 Activity 是 运行 还是已暂停。
基本上你问的是ipc。 Google 获取更多信息。
为此,您需要创建一个服务 class 并将两个活动绑定到它。
示例服务 class 如下所示。
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()
}
}
现在事情简单了。
现在您必须将第一个 activity 与服务绑定。
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
}
现在第二个activity
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);
}
}
就是这样。根据要求修改这个。
P.S.Above 提到的代码只是工作结构。
如果我在另一个进程的 Activity 上启动 Activity() ;新Activity真正启动的起点(启动新Activity形成后台服务)有没有好的方法接收回调?我的意思是我可以进行广播,但这似乎很蹩脚。有更好的方法吗?
如果您在同一个或另一个进程中的 Activity 上执行 startActivity()(并假设第二个 activity 开始),调用 Activity 将继续进入先 PAUSED,然后 STOPPED 状态。这意味着不会处理回调。但是,您可以调用 startActivityForResult() 而不是 startActivity() 并接收 onActivityResult() 回调。
添加一个 class MyApplication 扩展应用程序并在您的清单中提及,然后在其中放置两个布尔变量。
private static boolean activityVisible;
public static void activityResumed() {
activityVisible = true;
}
public static void activityPaused() {
activityVisible = false;
}
public static boolean isActivityVisible() {
return activityVisible;
}
所以现在您可以在当前 activity 的 onPause() 和 onStop() 上执行 startActivity() 时调用 activityPaused(),以及当您return 回到原来的状态 activity 在覆盖的 onResume() 方法中调用 activityResumed()。
现在,通过使用 MyApplication.isActivityVisible(),您可以了解您的 Activity 是 运行 还是已暂停。
基本上你问的是ipc。 Google 获取更多信息。
为此,您需要创建一个服务 class 并将两个活动绑定到它。
示例服务 class 如下所示。
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()
}
}
现在事情简单了。
现在您必须将第一个 activity 与服务绑定。
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
}
现在第二个activity
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);
}
}
就是这样。根据要求修改这个。
P.S.Above 提到的代码只是工作结构。