简体   繁体   English

如何在另一个活动中调用此方法?

[英]How can I call this method in another activity?

In my main Activity, I have this method: 在我的主要活动中,我有以下方法:

 public void CancelAlarms() {

            AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            manager.cancel(pendingIntent);
        }

but I want to call it in my other activity. 但我想在其他活动中称呼它。

How can I call it from one activity to another? 如何从一种活动称呼它为另一种活动? Also, would it be alright if I am using context in the method? 另外,如果我在方法中使用上下文,可以吗? Would the context change if I call it from another activity to this one? 如果我从另一个活动调用此上下文,该上下文会改变吗?

If you want to make public void CancelAlarms() available in all Activity then create a Base Activity class implement this method in that Activity now extend all your Activity with Base Activity class than you can call this method from any Activity which has extended with Base Activity. 如果要在所有Activity中提供public void CancelAlarms() ,则创建一个Base Activity类,实现该方法,因为该Activity现在可以使用Base Activity类扩展您的所有Activity,而您可以从通过Base Activity扩展的任何Activity中调用此方法。 Or put public void CancelAlarms() method in Application class. 或将public void CancelAlarms()方法放在Application类中。 Now using getApplicationContext() you can call this method in Any Activity or Service. 现在,使用getApplicationContext()可以在任何活动或服务中调用此方法。

You should probably move this method to a utility class or to a base Activity class, for exactly the reason you mention: it depends on a Context . 出于您提到的确切原因,您可能应该将此方法移至实用程序类或基本Activity类中:它取决于Context

You simply won't be able to invoke this method without having a reference to an instance of the Activity it is in, because it is not marked static . 如果没有引用该方法所在的Activity实例,您将无法调用该方法,因为该实例未标记为static And even if you did, it would use its host Activity --probably not what you intend. 即使您这样做了,它也将使用其宿主的Activity可能不是您想要的。

Instead, you could make a utility class containing the method. 相反,您可以制作一个包含该方法的实用程序类。 There are two ways to go about this: you could pass the Context in to the function every time, or you could pass a Context once to the class's constructor. 有两种解决方法:您可以每次将Context传递给函数,也可以一次将Context传递给类的构造函数。 The former is probably better for one-off convenience methods grouped into a Utilities class, while the latter is a bit more Java-like and is better if the class has a definite function (like an ActivityAlarmWrangler ). 前者对于将一次性便利方法分组到Utilities类中可能更好,而后者则更像Java,并且如果该类具有确定的函数(如ActivityAlarmWrangler )则更好。

Finally, you could move the function to a base class. 最后,您可以将函数移至基类。 This will be cleanest for simple cases because you don't have to have a different member object in your Activity , but if your Activity already extends a custom superclass, you won't be able to use this method unless you are willing to modify the superclass. 对于简单的情况,这是最干净的方法,因为您的Activity不必具有其他成员对象,但是如果您的Activity已经扩展了自定义超类,则除非您愿意修改该方法,否则将无法使用此方法。超类。 Because Java doesn't have multiple inheritance (which would be useful in this case), inheriting this method could get hairy fairly quickly. 因为Java没有多重继承(在这种情况下将很有用),所以继承此方法可能会很快变得毛茸茸。

Create a static instance of/in main activity. 创建主要活动的静态实例。

static MainActivity mainActivity; 静态MainActivity mainActivity;

Initialize it in oncreate method 用oncreate方法初始化它

@Override @Override
protected void onCreate(Bundle savedInstanceState) { 受保护的void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(....); setContentView(....);
mainActivity= this; mainActivity = this;
} }

and make a method which returns this instance. 并创建一个返回该实例的方法。

public static MainActivity getInstance() { 公共静态MainActivity getInstance(){
return mainActivity; 返回mainActivity;
} }

Now from other activity you can simply call your method as following : 现在,您可以从其他活动中简单地调用您的方法,如下所示:

MainActivity.getInstance().CancelAlarms(); MainActivity.getInstance()。CancelAlarms();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM