简体   繁体   中英

How pass string variable java from one method to another method

I am new on Java android code, please help me how to access String variable from one method to another method, this is from my code, I want to use variable deviceToken, to pass the value to another method that using javascriptinterface

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(String token) {
        Log.d("TokenFcm",token);

        String deviceToken = token ;

    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
    }
  1. by declaring deviceToken as instance variable, it can be accessed from anotherMethod of same class
public class MyFirebaseMessagingService extends FirebaseMessagingService {

String deviceToken = "";

    @Override
    public void onNewToken(String token) {
        Log.d("TokenFcm",token);

         deviceToken = token ;

    }
    
    public void anotherMethod(){
     System.out.println(deviceToken);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
    } ```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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