0

如何获取广播方法变量?

广播中只有一个变量。这是我的部分代码:

public void onCreate(Bundle savedInstanceState) {  
       super.onCreate(savedInstanceState);  
       setContentView(R.layout.main);  
       //I want to use a in here like textview.settext(a);
             IntentFilter intent = new IntentFilter();  
             registerReceiver(searchDevices, intent);
     }  
     private BroadcastReceiver searchDevices = new BroadcastReceiver() {      
     public void onReceive(Context context,  Intent intent) {  
               int a=0;
      }
     }
4

1 回答 1

1

您可以将可打包变量放入 Intent 实例:

Intent i = new intent();
i.putExtra("a", 0);

然后在您的接收器代码中,您将获得值:

int a = i.getIntExtra("a");

该文档可能会有所帮助。

于 2012-11-09T02:56:31.097 回答