安卓 - 新意向启动特定方法
我想启动我的一个现有活动,并强制该活动在启动后调用特定方法。这可能吗?
我可以定义一个在我的内部创建活动后应该调用的方法吗?Intent
例如,类似下面的内容:
Intent intent = new Intent(this, com.app.max.Home.class.myMethod);
我想启动我的一个现有活动,并强制该活动在启动后调用特定方法。这可能吗?
我可以定义一个在我的内部创建活动后应该调用的方法吗?Intent
例如,类似下面的内容:
Intent intent = new Intent(this, com.app.max.Home.class.myMethod);
不,我不认为你可以有这样的东西:
Intent intent = new Intent(this, com.app.max.Home.class.method);
但你可以这样做:
Intent intent = new Intent(this, com.app.max.Home.class);
intent.putExtra("methodName","myMethod");
startActivity(intent);
然后在被调用的活动中(您需要启动该方法的位置),您可以采取意图并决定调用哪个方法,如下所示:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent.getStringExtra("methodName").equals("myMethod")){
mymethod();
}
}
您好,您不能从意图调用特定方法,但也可以从意图使用服务,您的要求将完成。
Intent intent = new Intent(this, MyService.class);
和 MyService 中.class
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
yourParticularMethod(); //do your task and stop the service when your task is done for battery saving purpose
context.stopService(new Intent(context, MyService.class));
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
并在清单文件中注册此服务,如下所示
<application>
<service android:name=".MyService" />
.
.
</application>