Passing data between activities and intents

Put your problem here if it does not fit any of the other categories.

Passing data between activities and intents

Postby icydash » Sun Sep 11, 2011 2:38 am

Hey guys!

So my problem deals with grabbing data from a notification.

In my main activity, at certain times, it calls this function which creates a notification:

Code: Select all
    public void makeNotification(String name, String ranNum)
    {
       final NotificationManager mNotificationManager;
       final int ID = Integer.parseInt(ranNum);
      
       mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
       final Notification notifyDetails = new Notification(R.drawable.icon,"Notification Alert",System.currentTimeMillis());      
      
       CharSequence contentTitle = "new notification!";
       CharSequence contentText = "Click here to contact " + name + "!";
      
       Intent notifyIntent = new Intent(this, NotificationReceiver.class);
       PendingIntent intent = PendingIntent.getActivity(uNear.this, 0, notifyIntent, 0);
       notifyDetails.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, intent);
       mNotificationManager.notify(ID, notifyDetails);
    }


This code works perfectly. Upon clicking the notification, it calls my class NotificaitonReceiver, which has the following code:
Code: Select all
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class NotificationReceiver extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
        requestWindowFeature(Window.FEATURE_NO_TITLE);
      
         final Dialog NEWdialog = new Dialog(this);
         NEWdialog.setContentView(R.layout.callortextoption);
         NEWdialog.setTitle("uNear:  Contact User");
         NEWdialog.setCancelable(false);
         NEWdialog.show();
        
         Button cancelbutton = (Button) NEWdialog.findViewById(R.id.cancelIt);
           cancelbutton.setOnClickListener(new OnClickListener() {
           @Override
            public void onClick(View v)
           {
              finish();
           }
        });
          
         Button call = (Button) NEWdialog.findViewById(R.id.callNum);
           call.setOnClickListener(new OnClickListener() {
           @Override
            public void onClick(View v)
           {
               String num = "tel:8885551111";
               Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(num));
               startActivity(callIntent);
           }
        });
          
         Button text = (Button) NEWdialog.findViewById(R.id.textNum);
           text.setOnClickListener(new OnClickListener() {
           @Override
            public void onClick(View v)
           {
               String num = "sms:8885551111";
               Intent textIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(num));
               textIntent.putExtra("sms_body", "Hi!");
               startActivity(textIntent);
           }
        });
   }
}


The XML looks like this:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="160px">

<TextView android:id="@+id/callortext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="How would you like to contact this user?"  android:paddingTop="15px"/>

<Button android:layout_width="120px" android:text="Call" android:layout_height="wrap_content" android:id="@+id/callNum" android:layout_alignParentBottom="true" android:layout_marginLeft="10dp"></Button>
<Button android:layout_width="120px" android:text="Text" android:layout_height="wrap_content" android:id="@+id/textNum" android:layout_alignParentBottom="true" android:layout_marginLeft="98dp"></Button>
<Button android:layout_width="120px" android:text="Cancel" android:layout_height="wrap_content" android:id="@+id/cancelIt" android:layout_alignParentBottom="true" android:layout_marginLeft="187dp"></Button>

</RelativeLayout>


As you may have guessed, when you click the notification, it calls NotificationReceiver which makes a dialog popup asking how you would like to contact the user, with options for text messaging and phone calling (and a cancel button). This all executes perfectly.

My problem is this: Right now the phone number is hard coded in as 8885551111, but I would like that number to be variable. To do this, I need to grab the name in the notification, as all names are stored in my application's database with corresponding phone numbers. So, essentially, I must have NotificationReceiver get the name in the notification so that I can open the database, grab the corresponding phone number, and use that number for the calling / texting options. How can I do this? How do I make the NotificaitonReceiver class grab the name in the notification?

I hope my question makes sense haha.

Thanks!
icydash
Developer
Developer
 
Posts: 35
Joined: Thu Jun 09, 2011 1:22 am

Top

Re: Passing data between activities and intents

Postby Dockland » Sun Sep 11, 2011 9:36 am

How do I make the NotificaitonReceiver class grab the name in the notification?

Pass it in the intent.
Maybe something like.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent notifyIntent = new Intent(this, NotificationReceiver.class);
  2. notifyIntent.putExtra("whatever_id", the_string);
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


in NotificationReceiver
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = getIntent(); // returns an intent which started this activity. in this case it is the notifyIntent
  2. String the_string = intent.getExtras().getString("whatever_id");
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Dockland
Developer
Developer
 
Posts: 37
Joined: Wed Jul 27, 2011 12:29 pm

Re: Passing data between activities and intents

Postby icydash » Sun Sep 11, 2011 3:39 pm

Dockland wrote:
How do I make the NotificaitonReceiver class grab the name in the notification?

Pass it in the intent.
Maybe something like.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent notifyIntent = new Intent(this, NotificationReceiver.class);
  2. notifyIntent.putExtra("whatever_id", the_string);
  3.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


in NotificationReceiver
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = getIntent(); // returns an intent which started this activity. in this case it is the notifyIntent
  2. String the_string = intent.getExtras().getString("whatever_id");
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Worked perfectly! Thanks so much for taking the time to answer my question.
icydash
Developer
Developer
 
Posts: 35
Joined: Thu Jun 09, 2011 1:22 am

Re: Passing data between activities and intents

Postby icydash » Mon Sep 12, 2011 1:08 am

Dockland wrote:
How do I make the NotificaitonReceiver class grab the name in the notification?

Pass it in the intent.
Maybe something like.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent notifyIntent = new Intent(this, NotificationReceiver.class);
  2. notifyIntent.putExtra("whatever_id", the_string);
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


in NotificationReceiver
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = getIntent(); // returns an intent which started this activity. in this case it is the notifyIntent
  2. String the_string = intent.getExtras().getString("whatever_id");
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

What about for situations where there are multiple notifications, each with a different name in it? In other words, the notification function may be called 3-4 times back to back and have different names in the notification based on the circumstances, and I need to be able to grab the correct name when the intent (notification) is pressed.

Using the method above, each time the intent is called, it overwrites the contents of the variable "whatever_id", so when I click any of the notifications, it always displays the same name.

Do you possibly have any suggestions?
icydash
Developer
Developer
 
Posts: 35
Joined: Thu Jun 09, 2011 1:22 am

Re: Passing data between activities and intents

Postby Dockland » Mon Sep 12, 2011 2:07 pm

Maybe something like.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      private int m_notifyId = 0;
  2.     private void noti() {
  3.         m_notifyId++;
  4.         if (m_notifyId == Integer.MAX_VALUE) // ha ha
  5.                 m_notifyId = 1;
  6.         NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  7.         Notification noti = new Notification(android.R.drawable.ic_menu_agenda, "Notification Alert", System.currentTimeMillis());
  8.        
  9.         Intent notifyIntent = new Intent(this, NotificationReceiver.class);
  10.         notifyIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  11.         notifyIntent.putExtra("test", "notification");
  12.         double d = m_random.nextDouble();
  13.         notifyIntent.putExtra("double", d);
  14.         // despite that docs says that requestCode is not used in getActivity
  15.         // it is actually used. Try with same requestCode.
  16.         PendingIntent pendingIntent = PendingIntent.getActivity(this, m_notifyId, notifyIntent, PendingIntent.FLAG_ONE_SHOT);
  17.         noti.setLatestEventInfo(getApplicationContext(), (CharSequence)"Notification", (CharSequence)("Notification: " + d), pendingIntent);
  18.  
  19.         noti.flags |= Notification.FLAG_AUTO_CANCEL;
  20.         nm.notify(m_notifyId, noti);
  21.     }
  22.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1.     private TextView m_tvInfo;
  2.         @Override
  3.         public void onCreate(Bundle savedInstanceState) {
  4.                 super.onCreate(savedInstanceState);
  5.                 setContentView(R.layout.notification_receiver_main);
  6.                
  7.                 m_tvInfo = (TextView) findViewById(R.id.tvInfo);
  8.                
  9.                 Intent intent = getIntent();
  10.                            
  11.                 Log.d("log", "onCreate getIntent");
  12.                             // check if extras contains the key.
  13.                 String test = intent.getExtras().getString("test");
  14.                 test += "\n" + Double.toString(intent.getExtras().getDouble("double"));
  15.                 m_tvInfo.setText(test);
  16.                
  17.         }
  18.        
  19.         @Override
  20.         protected void onNewIntent(Intent intent) {
  21.                       setIntent(intent); // maybe not needed. depends on the activity logic.
  22.                 Log.d("log", "onNewIntent");
  23.                 String test = intent.getExtras().getString("test");
  24.                 test += "\n" + Double.toString(intent.getExtras().getDouble("double"));
  25.                 m_tvInfo.setText(test);
  26.         }
  27.  
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


It is only imho.
Dockland
Developer
Developer
 
Posts: 37
Joined: Wed Jul 27, 2011 12:29 pm

Re: Passing data between activities and intents

Postby icydash » Tue Sep 13, 2011 4:31 am

Cool. Thanks!
icydash
Developer
Developer
 
Posts: 35
Joined: Thu Jun 09, 2011 1:22 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 13 guests