protected void onMessage(Context context, Intent intent) {
String msg = intent.getStringExtra("payload");
String newspushid1= intent.getStringExtra("payload1");
gc.setnews_pushid(newspushid1);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
BackgroundAlert bg = new BackgroundAlert(mNotificationManager, msg);
bg.onReceive(getApplicationContext(), intent);
}
In this i am Getting value of msg and Push id (helloworld and 101 respectively) i want get this value in another activity .
if (gdata.getPush_message() != null) {
String push_id_value = gdata.getnews_pushid();
Log.e("CATID", "ID-->" + push_id_value);
}
I have apply this code for getting value .from GCMIntentService class but i am getting same value push_id_value ,getPush_message in another activity which is hello world i am unable to get value 101 in another activity please check my code tell me solution .
Create a Constant Class like :
public class Constant {
public static String PAYLOAD = "";
public static String PAYLOAD_1 = "";
}
Now, you can set this value in Activity1 like
Constant.PAYLOAD = intent.getStringExtra("payload");
Constant.PAYLOAD_1= intent.getStringExtra("payload1");
Now, get this value in Activity2 like:
String push_id_value = Constant.PAYLOAD;
String push_id_value1 = Constant.PAYLOAD_1;
And/or for Preference go to my this answer:Android. How to save user name and password after the app is closed?
In your current Activity,
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
Use this technique to pass values from one Activity to another.
Related
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 2 years ago.
I have made the code and there's no error, but logcat shows a NullPointerException. This is my code to pass and to catch the value from each activity. Please help me solve this.
First activity:
try {
Intent in = new Intent();
in.setClass(p_daftar_pelanggan.this, ubah_data_pelanggan.class);
in.putExtra("id", eid.getText().toString());
in.putExtra("nama", tnama.getText().toString());
in.putExtra("alamat", ealamat.getText().toString());
in.putExtra("no_hp", ehp.getText().toString());
startActivity(in);
}
catch(NullPointerException ex)
{
}
Second activity:
Bundle b = getIntent().getExtras();
String idpgu = b.getString("id");
String namapgu = b.getString("nama");
String alamatpgu = b.getString("alamat");
String hppgu = b.getString("no_hp");
idp.setText(idpgu);
idp.setKeyListener(null);
nama.setText(namapgu);
ealamat.setText(alamatpgu);
hp.setText(hppgu);
Try the following in the second activity:
Intent intent = getIntent();
String idpgu = intent.getStringExtra("id");
You can use Bundle for this purpose:
Activity destActivity = new Activty();
Bundle b = new Bundle();
destActivity.putString("TAG_ForValue",YourValue.getText().toString());
destActivity.setArguments(b);
Intent intent = new Intent(startActivity.this, destActivity.class);
startActivity(intent);
And in your destActivity :
Bundle bundle = getArguments();
if(bundle !=null)
String value = bundle.getString("TAG_ForValue");
You are sending and receiving the values correctly, but probably some values are null, you can validate setting an empty string instead of a null value:
Bundle b = getIntent().getExtras();
String idpgu = (b.getString("id") != null)? b.getString("id") : "";
String namapgu = (b.getString("nama") != null)? b.getString("nama") : "";
String alamatpgu = (b.getString("alamat") != null)? b.getString("alamat") : "";
String hppgu = (b.getString("no_hp") != null)? b.getString("no_hp") : "";
You put extras using Intent in FirstActivity But Use Bundle to receive it, that is why you have NullPointerException because SecondActivity is looking for values in different place.
Do the following in your secondActvity
Intent intent = getIntent();
String idpgu = intent.getStringExtra("id");
String namapgu = intent.getStringExtra("nama");
String alamatpgu = intent.getStringExtra("alamat");
String hppgu = intent.getStringExtra("no_hp");
// Check Null for each Values e.g.
if (idpgu.trim().equals(" "))
{ idpgu = "null";
}
I have a minor problem that I'm stuck. The problem is that I had passed a id with the code below.
Intent i = new Intent(First.this,Second.class);
i.putExtra("classification_id","3");
However, when I try to get the parameter of 3 with the code below I get the result of -1 when I check it in the debug mode.
if(intent.getExtras().getString("classification_id")!=null){
classId = intent.getExtras().getString("classification_id");
}else{
classId = "1";
}
Actually I want to use this parameter to set it into a url to get the json data to get the json data . But Is this a right way? Or is it a bad practice to set the String int into a url? Ex. "www.test.test/myid?="+classId
Where is the intent coming from ? There are getIntent() or Intent coming from methods like onNewIntent()
Also I think this is shorter
if(getIntent().hasExtra("classification_id")) {
String classId = getIntent().getStringExtra("classification_id");
}
As for inserting String into url, you will be overwhelmed if there are many parameters (btw, I think this is a wrong format: www.example.test/myid?=classId maybe this is what you want www.example.com/test?myid=classId ). So we can do
private static final String URL="https://www.example.com";
private static final String PATH = "test";
private static final String PARAM_MYID = "myid";
public static String buildMyUrl(String id){
Uri.Builder b = Uri.parse(URL).buildUpon();
b.path(PATH);
b.appendQueryParameter(PARAM_MYID, id);
b.build();
return b.toString();
}
Intent in = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
startActivity (in);
i wan to send SongName to next Activity
Try this in your first activity,
Intent intent = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
intent.putExtra("message", SongName);
startActivity(intent);
and then in second activity get your string like below
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
then set this string to text-view (This is not necessary but just for your reference, if you want to check whether you got correct string or not)
TextView txtView = (TextView) findViewById(R.id.your_resource_textview);
txtView.setText(message);
use this line before startActivty :
intent.putExtra("SongName",SongName);
then retrieve it from the target activty.
just use
first activity to send :
Intent in = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
in.putExtra("data",SongName);
startActivity (in);
to receive in another activity:
String Sname = getIntent().getExtras().getString("data");
to see the details of intents refer here : http://developer.android.com/guide/components/intents-filters.html
I'm trying to send some data from one activity to another and it's sorta working but not like I want to work.
Problem 1-Things are getting mixed up. On the Next Activity part of the listitem is going to an incorrect textView and part to the correct textview.
Problem 2- I am only able to list 1 item on the new activity but I want to be able to send multiple listitems. I think the problem lies in combining different types of putExtra request to the same place like I do here.
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
Ant help would be appreciated.
Sending Data to next Activity
final TextView username =(TextView)findViewById(R.id.resultTextView);
String uname = username.getText().toString();
final TextView uplane =(TextView)findViewById(R.id.inputPrice);
String pick = uplane.getText().toString();
final TextView daplane =(TextView)findViewById(R.id.date);
String watch = daplane.getText().toString();
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putExtra("Card Number",(CharSequence)uname)
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
);
finish();
This is the Next Activity
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getCharSequenceExtra("Card Number"));
}
Intent it = getIntent();
if (it.getCharSequenceExtra("date") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleTime);
setmsg.setText(it.getCharSequenceExtra("date"));
}
Intent id1 = getIntent();
if (id1.getCharSequenceExtra("inputPrice") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleName);
setmsg.setText(id1.getCharSequenceExtra("inputPrice"));
}
ArrayList<String> al= new ArrayList<String>();
al = getIntent().getExtras().getStringArrayList("list");
saleNotes= (TextView) findViewById(R.id.saleNotes);
saleNotes.setText(al.get(0));
Alright, a few things:
First of all you do not need to cast your strings as CharSequence.
Second thing,
Define intent, add your extras and only then call startActivity as below:
Intent intent = new Intent(MenuView1Activity.this,RecordCheckActivity.class);
intent.putExtra("date", watch);
startActivity(intent);
Third, when retrieving the intent create a bundle first as below:
Bundle extras = getIntent().getExtras();
String date = extras.getString("date");
EDIT:
Here is how you convert your entire array list to one single string and add it to your textview.
String listString = "";
for (String s : al)
{
listString += s + "\t"; // use " " for space, "\n" for new line instead of "\t"
}
System.out.println(listString);
saleNotes.setText(listString);
Hope this helps!
Try this, Don't use CharSequence just put string value
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",watch)
.putExtra("Card Number",uname)
.putExtra("inputPrice",pick)
.putStringArrayListExtra("list", listItems)
);
And get like this
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getStringExtra("Card Number"));
}
This question already has answers here:
How to pass a value from one Activity to another in Android? [duplicate]
(7 answers)
Closed 9 years ago.
i would like to send the values from one activity to another but i got null pointer
exception please solve my problem.the first activity contains sms the details of that
sms is send to second activity based on these values th esecond activity search contact
no and send reply sms.
public void onReceive( Context context, Intent intent )
{
// Get SMS map from Intent
Bundle bundle = null;
Bundle extras = intent.getExtras();
String messages = "";
String address = null,body=null;
if ( extras != null )
{
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get( "pdus" );
// Get ContentResolver object for pushing encrypted SMS to incoming folder
//ContentResolver contentResolver = context.getContentResolver();
for ( int i = 0; i < smsExtra.length; ++i )
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
body = sms.getMessageBody().toString();
address = sms.getOriginatingAddress();
messages += "SMS from " + address + " :\n";
messages += body + "\n";
// Here you can add any your code to work with incoming SMS
// I added encrypting of all received SMS
}
// Display SMS message
Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
Intent i=new Intent(context,AlertActivity.class);
bundle.putString("from",address);
bundle.putString("msg",body);
i.putExtras(bundle);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Activity2:
Intent i=getIntent();
Bundle bundle=i.getExtras();
String fromAdd=bundle.getString("from");
String msgBody=bundle.getString("body");
Change this
String msgBody=bundle.getString("body");
to
String msgBody=bundle.getString("msg");
in Android Bundle we put key value pair, and it is mandatory that you pass same key, while getting data from bundle, which you are putting data. check your code you are putting two string on intent, which are:
"from", and "msg"
and you are getting values from intent by key:
"from" and "body"
so change it either in starting activity or Activity 2. so that key values would match.
try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle bundle = this.getIntent().getExtras();
if (bundle != null)
{
String fromAdd = bundle.getString("from");
String msgBody = bundle.getString("body");
}
}
Try this.....
Activity1.java
Intent intent = new Intent(getApplication(),Activity2.class);
intent.putExtra("from",from);
intent.putExtra("Body",Body);
StartActivity(intent);
Activity2.java
Intent intent = getintent();
Bundle bundle=intent.getExtras();
String Body=bundle.getString("Body");
String From=bundle.getString("From");
setResult("RESULT_OK",intent);
try with this way..
Bundle bu=getIntent().getExtras();
String title=bu.get("from").toString();
String msg=bu.get("body").toString();
Try this..... to pass value from Activity1 to Activity2.
Intent myIntent = new Intent(Activity1.this, Activity2.class);
myIntent.putExtra("UserId",UserId);
myIntent.putExtra("UserName",UserName);
myIntent.putExtra("CompanyID",CompanyID);
myIntent.putExtra("CompanyName",CompanyName);
myIntent.putExtra("ProjectId",ProjectId);
startActivity(myIntent);
Also can extract the value you can use
Intent intent = getIntent();
UserId=intent.getStringExtra("UserId");
UserName=intent.getStringExtra("UserName");
CompanyID=intent.getStringExtra("CompanyID");
CompanyName=intent.getStringExtra("CompanyName");