简体   繁体   中英

Pass data from activity to service ? Android

How many I can pass data to Service by Intent . I am passing 7 data to Service but the latter two is null .

This code is in MainActivity.class :

    Intent Class = new Intent(MainActivity.this, Class_Service.class);
    Class.putExtra("Url", URL);
    Class.putExtra("Username", Username);
    Class.putExtra("PASSWORD", PASSWORD);
    Class.putExtra("Mobile", Mobile);
    Class.putExtra("HQStudentID", HQStudentID);
    Class.putExtra("_SchoolCode", SchoolCode);
    Class.putExtra("_GroupID", GroupID);

And here is in my Service :

public class Class_Service extends Service{
    String Webresponse;
    String URL ;
    String Username;
    String PASSWORD;
    String Mobile;
    String HQStudentID;
    String SchoolCode;
    String GroupID;


    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        handleStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handleStart(intent, startId);
        return START_NOT_STICKY;
    }

    void handleStart(Intent intent, int startId) {
        URL = intent.getStringExtra("Url");
        Username = intent.getStringExtra("Username");
        PASSWORD = intent.getStringExtra("PASSWORD");
        Mobile = intent.getStringExtra("Mobile");
        HQStudentID = intent.getStringExtra("HQStudentID");
        GroupID = intent.getStringExtra("_GroupID");
        SchoolCode = intent.getStringExtra("_SchoolCode");

        Log.i("test", "HERE : : :" + GroupID + "::" + SchoolCode);
    } 
}

All of parameter have value but GroupID and SchoolCode are null !!!

When I pass GroupID and SchoolCode from MainActivity have value.

There are 2 things that may cause the issue :

1 : You are trying to send an integer and trying to get it as String on Service. Check the data types and fix if there is anything wrong.

2 : The data may be null. Check all datas before putExtra on Logcat to see all of them has some values.

Hope it helps.

Best, Mack.

如果要传递整数,请使用intent.getIntExtras(“ tag”),并使用字符串intent.getStringExtra(“ tag”)。

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