简体   繁体   English

如何将意图数据从主项目发送到android中的库项目?

[英]How to send intent data from main project to library project in android?

I have been trying to send data from my main project to my library project.我一直在尝试将数据从我的主项目发送到我的图书馆项目。 My library project is defined in main project so no problem of adding dependency of lib project.one more thread I have found similar to my problem but no luck so far... thread我的库项目是在主项目中定义的,所以添加 lib 项目的依赖没有问题。我发现还有一个线程与我的问题类似,但到目前为止没有运气...... 线程

GetUserprofile is the main project's activity and LockscreenActivity is library project's activity. GetUserprofile是主项目的活动, LockscreenActivity是库项目的活动。

I have written code on the main project side我在主项目端写了代码

Intent myIntent = null;
    try {
        myIntent = new Intent(GetUserprofile.this,LockscreenActivity.class);

       myIntent.putExtra("examiddetails",exam_id);

        startActivity(myIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }

Now I am getting the data which is examiddetails on the library project by using below code现在我使用以下代码获取库项目中的examiddetails数据

Intent intent = getIntent();    
    try{
        if(intent != null){
            String Examid = intent.getStringExtra("examiddetails");
        }

    }catch(Exception e){
        System.out.println("getdataFromService exception"+e.toString());
    }

I am getting null value of Examid String on the library project.我在库项目中得到了 Examid String 的空值

Create Intent From your application从您的应用程序创建意图

Intent myIntent= new Intent(this,YourActivity.class)
myIntent.putExtra("data", "value");

Get the Intent From Library从图书馆获取意图

string intentdata = ((Activity)getContext()).getIntent().getStringExtra("data");

Do you mean send data to another activity?你的意思是将数据发送到另一个活动? I had made same before.我以前也做过。

MainProject class主项目类

Intent intent= new intent(getApplication(), LockscreenActivity.class);
intent.putExtra("examiddetails",exam_id);
startActivity(intent);

LockscreenActivity class LockscreenActivity 类

Bundle bundle = getIntent().getExtras();
String ExamIdDetails = bundle.getString("examiddetails");

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

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