Android Forums

Go Back   These FORUMS ARE DISABLED!! OUR NEW FORUMS ARE AT http://androidforum.com/ > Android Coders > Android Development, Answers, Tutorials, and Code Snippets
Connect with Facebook

Click Here To Register!
 
 
LinkBack Thread Tools Display Modes
Old 02-17-2009, 02:20 PM   #1
Junior Member
 
Join Date: Feb 2009
Posts: 5
Friends: 0
View deenybird's Profile   View deenybird's Photo Album   View deenybird's Blog   View Social Groups
What intents do I use to pass a variable to another class? And also which intent do I use to receive it?
thanks
deenybird is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 02-17-2009, 06:24 PM   #2
Junior Member
 
Join Date: Feb 2009
Posts: 7
Friends: 0
View ians's Profile   View ians's Photo Album   View ians's Blog   View Social Groups
You can create your own custom Intent and attach whatever you want to it. then create a BroadcastListener at the other end to receive it.

Intent i = new Intent("myintent");
i.putExtra("mydata", data);

---------

IntentFilter f = new IntentFilter();
f.addAction("myintent");
BroadcastReceiver r = new BroadcastReceiver() {
public void onReceive(Context c, Intent i) {
//do something
}};
context.registerReceiver(r, f);
ians is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 02-19-2009, 01:08 AM   #3
Junior Member
 
Join Date: Feb 2009
Posts: 5
Friends: 0
View deenybird's Profile   View deenybird's Photo Album   View deenybird's Blog   View Social Groups
Thanks for the info. Everything seemed to work except this code "context.registerReceiver(r, f);" which had an error. It wants context to be created as a local variable. If I delete it then the broadcast receiver says it isn't being used.
deenybird is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 02-19-2009, 09:59 AM   #4
Junior Member
 
Join Date: Feb 2009
Posts: 7
Friends: 0
View ians's Profile   View ians's Photo Album   View ians's Blog   View Social Groups
context points to a Context object. If you don't see this anywhere it is probably 'this' ... i.e. Activity objects are also Context objects.
ians is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 02-19-2009, 11:45 AM   #5
Junior Member
 
Join Date: Feb 2009
Posts: 5
Friends: 0
View deenybird's Profile   View deenybird's Photo Album   View deenybird's Blog   View Social Groups
Thanks ians for your response. I really appreciate it. "this" was what I needed to not get an error. I've posted the code to simplify this and was hoping you could let me know what I'm missing. I've included the code for both classes below. In the onItemSelected method I change arg2 to j which represents the selected item in the list view. I know this works because if you select the 4th item in the list it starts the startNewScreen method at the bottom of the first class. I put the intent code in as you wrote. I added the 'j' variable in the putExtra method of the intent and am trying to get the other class to reconize it so I can use it in an if statement in the second class.

public class MainScreen extends Activity {

public String [] array1={"G1", "iPhone", "Android", "apple", "Google"};

ListView listview1;
TextView txtview1;

int i;
public static int j;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

txtview1=(TextView)findViewById(R.id.textview1);

listview1=(ListView)findViewById(R.id.listview1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array1);
adapter.setDropDownViewResource(android.R.layout.s imple_list_item_1);
listview1.setAdapter(adapter);

listview1.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
j = arg2;

if (j > 3) {

startNewScreen();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});


}

public void startNewScreen () {
Intent variablepass = new Intent("myintent");
variablepass.putExtra("mydata", j);
Intent intent = new Intent(MainScreen.this, MainScreen2.class);
startActivity(intent);
}


}

__________________________________________________ _____________________________________________
Public class MainScreen2 extends Activity {

TextView textview2;
public static int k;
public int j;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main2);


textview2=(TextView)findViewById(R.id.textview2);
textview2.setText("Hello2");

IntentFilter f = new IntentFilter("myintent");
f.addAction("myintent");
BroadcastReceiver r = new BroadcastReceiver() {

public void onReceive(Context c, Intent variablepass) {

if (j>3) {
textview2.setText("Google");


};
}
};
this.registerReceiver(r,f);

}


}
deenybird is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 02-19-2009, 12:14 PM   #6
Junior Member
 
Join Date: Feb 2009
Posts: 7
Friends: 0
View ians's Profile   View ians's Photo Album   View ians's Blog   View Social Groups
you need to retrieve the data that you put into the intent.

in you onReceive method:

j = variablepass.getIntExtra("mydata")
ians is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 02-19-2009, 12:51 PM   #7
Junior Member
 
Join Date: Feb 2009
Posts: 5
Friends: 0
View deenybird's Profile   View deenybird's Photo Album   View deenybird's Blog   View Social Groups
Thanks ians. I actually had tried this but had gotten an error "The method getIntExtra(String, int) in the type Intent is not applicable for the arguments (String)" So I changed it to "j = variablepass.getIntExtra("mydata", j); that 'j' at the end was required (or at least a variable is) I tried putting in a different variable but that didn't seem to work.

Thanks again for your attention to this!
deenybird is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
 

Thread Tools
Display Modes




Unlocked G1 Phones | Buy T-Mobile G1 | Google Phone

All times are GMT -6. The time now is 03:51 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright (c) 2012 TalkAndroid.com. All rights reserved.