简体   繁体   English

Android:将变量传递给非活动类

[英]Android: Passing a variable to a non-activity class

I am trying to pass a String from my MainActivity to a non-activity class (A Notification Service) So that I can customise the notification message. 我试图将我的MainActivity中的String传递给非活动类(A Notification Service)以便我可以自定义通知消息。 I have tried using Intent however it won't work because it is a non-activity based class. 我尝试过使用Intent,但它不起作用,因为它是一个非基于活动的类。 What is the best why to do this? 这样做的最佳原因是什么?

I am currently trying to use a global variable but its not working. 我目前正在尝试使用全局变量,但它不起作用。

MainActivity.java MainActivity.java

private String var;
private String a = "Pumpkin";

public String getVar(){
    return var;
}
public void setVar(String a){
    var = a;
}

NotificationService.java NotificationService.java

  MainActivity b = ((MainActivity)getApplicationContext());
  String var = b.getVar();

This crashes the app when even I call the variable var in NotificationService.java 即使我在NotificationService.java中调用变量var,这也会使应用程序崩溃

1.You can try by making static variable, and call with Class refenrence in any where. 1.您可以尝试制作静态变量,并在任何地方使用Class refenrence进行调用。

 public static  String a = "Pumpkin";//Declare in MainActivity
 Use it: String var = MainActivity.a;

2. Pass in Intent as extras and then get extras in where intent recived. 2.将Intent作为额外内容传递,然后在意图收到的地方获得额外内容。

  startActivity(new Intent(MainActivity.this, PropertyListviewActivity.class).putExtra("Key", "Value"));

Recive : Recive:

 if (intent != null)
        if (intent.getExtras() != null)
            if (intent.getExtras().getString("Key") != null) {
                String recivedString= intent.getExtras().getString("Key");

3.Keep in Shared Preferences and retrive where ever you want(not good practise). 3.保持共享首选项并在任何你想要的地方(不是好的练习)。

4.If Not an Android Component class try pass in Contractor. 4.如果不是Android组件类,请尝试传递Contractor。

尝试活动巴士绿色机器人奥托活动巴士都是很好的选择。

You are trying to cast app Context to Activity. 您正在尝试将应用上下文转换为活动。 It crashes, cause app context is not an activity. 它崩溃,导致应用程序上下文不是活动。 You should use handleIntent or binder in your service to transport data. 您应该在服务中使用handleIntent或binder来传输数据。 http://developer.android.com/guide/components/bound-services.html#Basics http://developer.android.com/guide/components/bound-services.html#Basics

you can do this task by saving data to application level variable or save it in sharedpreference. 您可以通过将数据保存到应用程序级变量或将其保存在共享首选项中来执行此任务。

To save data in application level make a static variable like 要在应用程序级别保存数据,请创建一个静态变量

public static DatatypeYouDesire(String/int) cachedData;

now when you want to store data jst initialize this varaible with the data you want to store like 现在,当您想要存储数据时,jst使用您想要存储的数据初始化此变量

YourApplictionClass.cachedData = "your data to be stored";

similarly you can use this data by accesing YourApplicationClass.cacheData 类似地,您可以通过访问YourApplicationClass.cacheData来使用此数据

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

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