Call class java android

Call method from another class on Android

I have created two classes, actually both of them extends Activity. What I am trying to do is to call a method from the second class.

What I am trying to do is calling the method from second class then implemented in first class, unfortunately I did not have success in that.

I need your help to solve this problem. Thank you

The second class:

4 Answers 4

Make a simple normal java file then define these methods in that class.

Call methods of regular java file in activity like this.

Why should you do it?
Why Sounds class extending Activity?
Please read once again the official documentation Activity.

If you did it cause you need a context just pass it like a parameter to the Sounds class.

And you also need to visit Android development guide too

Only one Activity is instantiated at a time. You should not try to call one Activity from another.

Instead, you should create a third class which contains the method you want to call.

However, you are going to have to do more work with the MediaPlayer. You should read the documentation for it before proceeding. The code I’ve shown gives you the basics of what you need to do but it will not work.

Finally, best advice I can give you is to learn the basics of Java and OOP before you proceed.

Источник

Calling method of class one to another class in android java?

Heys devs I have a quick question I have a method which is present in my MainActivity now I want to call that method in my LoginActivity. I have done this thing through getInstance but it give me an error which I share in below:

My method of calling : In my MainActivity I use :

In login activity I use:

Any fix of this problem?

2 Answers 2

You need to declare that function as public and static and you can call them anywhere from the application. example

From the error logs it looks like you are trying to show a Dialog in MainActivity.checktag() function when LoginActivity is on the top. This is not allowed. The Dialog needs to be managed by the Activity which is on the top.

Updated: To clarify, the error is not due to calling the method of MainActivity from LoginActivity. The issue here is that your code is creating a Dialog with the MainActivity context when it is not the current active activity. If your intention is to reuse some code to show Dialog between MainActivity and LoginActivity, you could move the function to a new Utility class and then pass the current Activity instance to the function. The same could be done while keeping the code in MainActivity itself, but the former method looks cleaner in my opinion.

Читайте также:  Poco x3 будет ли андроид 11

Источник

How to call a method from android library class

I want to call a method from a android library class which i have imported as a androidlib.jar. As i am able to call a whole class of library but i dont want it, but i want to call a particular method of library class.

I tried something like this, but it is showing java.lang.Nullpointer exception

This is my library class (AndroidLiB.class), where i have imported its jar file

This is my application class where i want to call a method from above class

But it is not working

4 Answers 4

If you want simply a method from jar, then why you need to extends Activity. My suggestion is remove extends Activity will fix the NPE error.

And In your main class call like

I would do something like this:

You can extend library class. For example:

What you’re trying to do isn’t really the best way to do things, but I’m assuming that the question being asked is how to import the jar correctly. If so:

If using eclipse,

  1. Make sure the androidlib.jar file is in the libs folder.
  2. Right click on androidlib.jar and select Build Path > Add to Build Path
  3. Right click on your project folder, go to Properties > Java Build Path > Order and Export and then make sure androidlib.jar is selected.

The problem with your current code is that when you call getApplicationContext() , the Activity hasn’t been started yet, therefore there is no context. A quick and dirty solution would be to rewrite the startGPS() method like this:

But I would much rather put that method inside some sort of Utilities class or even inside a parent Activity class.

Источник

Call class java android

Represents an ongoing phone call that the in-call app should present to the user.

Summary

Nested Classes
Call.Callback
Call.Details
Constants
String AVAILABLE_PHONE_ACCOUNTS The key to retrieve the optional PhoneAccount s Telecom can bundle with its Call extras.
int STATE_ACTIVE The state of a Call when actively supporting conversation.
int STATE_CONNECTING The initial state of an outgoing Call .
int STATE_DIALING The state of an outgoing Call when dialing the remote number, but not yet connected.
int STATE_DISCONNECTED The state of a Call when no further voice or other communication is being transmitted, the remote side has been or will inevitably be informed that the Call is no longer active, and the local data transport has or inevitably will release resources associated with this Call .
int STATE_DISCONNECTING The state of a Call when the user has initiated a disconnection of the call, but the call has not yet been disconnected by the underlying ConnectionService .
int STATE_HOLDING The state of a Call when in a holding state.
int STATE_NEW The state of a Call when newly created.
int STATE_RINGING The state of an incoming Call when ringing locally, but not yet connected.
int STATE_SELECT_PHONE_ACCOUNT The state of an outgoing Call when waiting on user to select a PhoneAccount through which to place the call.
Public Methods

Constants

public static final String AVAILABLE_PHONE_ACCOUNTS

The key to retrieve the optional PhoneAccount s Telecom can bundle with its Call extras. Used to pass the phone accounts to display on the front end to the user in order to select phone accounts to (for example) place a call.

public static final int STATE_ACTIVE

The state of a Call when actively supporting conversation.

public static final int STATE_CONNECTING

The initial state of an outgoing Call . Common transitions are to STATE_DIALING state for a successful call or STATE_DISCONNECTED if it failed.

public static final int STATE_DIALING

The state of an outgoing Call when dialing the remote number, but not yet connected.

public static final int STATE_DISCONNECTED

The state of a Call when no further voice or other communication is being transmitted, the remote side has been or will inevitably be informed that the Call is no longer active, and the local data transport has or inevitably will release resources associated with this Call .

public static final int STATE_DISCONNECTING

The state of a Call when the user has initiated a disconnection of the call, but the call has not yet been disconnected by the underlying ConnectionService . The next state of the call is (potentially) STATE_DISCONNECTED .

public static final int STATE_HOLDING

The state of a Call when in a holding state.

public static final int STATE_NEW

The state of a Call when newly created.

public static final int STATE_RINGING

The state of an incoming Call when ringing locally, but not yet connected.

public static final int STATE_SELECT_PHONE_ACCOUNT

The state of an outgoing Call when waiting on user to select a PhoneAccount through which to place the call.

Public Methods

public void answer (int videoState)

Instructs this STATE_RINGING Call to answer.

Parameters
videoState The video state in which to answer the call.

public void conference (Call callToConferenceWith)

Instructs this Call to enter a conference.

Parameters
callToConferenceWith The other call with which to conference.

public void disconnect ()

Instructs this Call to disconnect.

public List getCannedTextResponses ()

Obtains a list of canned, pre-configured message responses to present to the user as ways of rejecting this Call using via a text message.

Returns
  • A list of canned text message responses.
See Also

public List getChildren ()

Obtains the children of this conference Call , if any.

Returns
  • The children of this Call if this Call is a conference, or an empty List otherwise.

public List getConferenceableCalls ()

Returns the list of Call s with which this Call is allowed to conference.

Returns
  • The list of conferenceable Call s.

public Call.Details getDetails ()

Obtains an object containing call details.

Returns
  • A Call.Details object. Depending on the state of the Call , the result may be null .

public Call getParent ()

Obtains the parent of this Call in a conference, if any.

Returns
  • The parent Call , or null if this Call is not a child of any conference Call s.

public String getRemainingPostDialSequence ()

Obtains the post-dial sequence remaining to be emitted by this Call , if any.

Returns
  • The remaining post-dial sequence, or null if there is no post-dial sequence remaining or this Call is not in a post-dial state.

public int getState ()

Obtains the state of this Call .

Returns
  • A state value, chosen from the STATE_* constants.

public InCallService.VideoCall getVideoCall ()

Obtains an object that can be used to display video from this Call .

Returns

public void hold ()

Instructs this Call to go on hold.

public void mergeConference ()

Merges the calls within this conference. See CAPABILITY_MERGE_CONFERENCE .

public void phoneAccountSelected (PhoneAccountHandle accountHandle, boolean setDefault)

Notifies this Call that an account has been selected and to proceed with placing an outgoing call. Optionally sets this account as the default account.

public void playDtmfTone (char digit)

Instructs this Call to play a dual-tone multi-frequency signaling (DTMF) tone. Any other currently playing DTMF tone in the specified call is immediately stopped.

Parameters
digit A character representing the DTMF digit for which to play the tone. This value must be one of ‘0’ through ‘9’ , ‘*’ or ‘#’ .

public void postDialContinue (boolean proceed)

Instructs this Call to continue playing a post-dial DTMF string. A post-dial DTMF string is a string of digits entered after a phone number, when dialed, that are immediately sent as DTMF tones to the recipient as soon as the connection is made. If the DTMF string contains a DTMF_CHARACTER_PAUSE symbol, this Call will temporarily pause playing the tones for a pre-defined period of time. If the DTMF string contains a DTMF_CHARACTER_WAIT symbol, this Call will pause playing the tones and notify callbacks via onPostDialWait(Call, String) . At this point, the in-call app should display to the user an indication of this state and an affordance to continue the postdial sequence. When the user decides to continue the postdial sequence, the in-call app should invoke the postDialContinue(boolean) method.

Источник

How To Create New Java Class In Android Studio

In android development, the java class is commonly used to implement business logic processes such as database access, send emails, send short messages etc. We always use one java class to encapsulate some related functions.

So we can think java class as the Model role in the MVC pattern. This example will show you how to create a new java class in android studio, and we will create a SMSBean java class that will implement short message send and receives functions.

1.How To Create New Java Class In Android Studio.

  1. Launch Android Studio, create a new project. You can read the article How To Create New Android Studio Project to learn more.
  2. After that right-click the default package in the android studio Project View’s Android subview, then click the New —> Java Class menu item.
  3. You can also click the New —> Package menu item to create a new package or sub-package under exist package. In this example, we create a sub-package model under the existing com.dev2qa.example package.
  4. Then there will popup a dialog that will let you input java class-related information. Input class name SMSBean in the Name input text box, select Class in the Kind drop-down list, do not input any superclass, do not implement any interface. Input package name value such as com.dev2qa.example.model. Click the OK button.
  5. After clicking the OK button, the java class SMSBean has been created successfully. We will add two methods in it, one is sendSMS(), the other is receiveSMS(), please see code comments for detail.

2. Java Class SMSBean Usage Example.

This example will use SMSBean as a short messages manager object, it has two methods sendSMS() and receiveSMS().

The user interface of this example is not complex, there will have two buttons on the screen one is used to send short messages, the other is used to receive short messages. When you click any of the two buttons, an AlertDialog will pop up showing some messages.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/VhM3qIQkuD8

So if you want to run this example, you should create a new activity and layout XML files to implement the user interface. You can read the below articles if you do not know how to create activity and use event listener.

Источник

Читайте также:  Android studio kotlin livedata
Оцените статью