java - How can I call this method in another activity? -
in main activity, have 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 want phone call in other activity.
how can phone call 1 activity another? also, alright if using context in method? context alter if phone call activity one?
you should move method utility class or base of operations activity
class, reason mention: depends on context
.
you won't able invoke method without having reference instance of activity in, because not marked static
. , if did, utilize host activity
--probably not intend.
instead, create utility class containing method. there 2 ways go this: pass context
in function every time, or pass context
1 time class's constructor. former improve one-off convenience methods grouped utilities
class, while latter bit more java-like , improve if class has definite function (like activityalarmwrangler
).
finally, move function base of operations class. cleanest simple cases because don't have have different fellow member object in activity
, if activity
extends custom superclass, won't able utilize method unless willing modify superclass. because java doesn't have multiple inheritance (which useful in case), inheriting method hairy quickly.
java android
No comments:
Post a Comment