Andrew Stewart
Andrew Stewart

Reputation: 279

getIntent in broadcast receiver undefined android

In my app I have a pending intent that when broadcasts when its requirements are met this is listened for by a broadcast receiver. This broadcast receiver then does what I want it to do however I need to add extras from one activity to the other and I am using intent.putExtra to do this. `

However, within my Receiver class getIntent() method is undefined. How does it work when using it with pending intents as it has worked with all my other intents.

This is my targetdistance.class: (sets up broadcast and pending intent)

package com.example.drivetext;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;

public class targetdistance extends Activity {

double finalc1;
double finalc2;
int finalsd;
String finalmessage;
String finalnumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.target);

    LocationManager locationManager;
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    String provider = LocationManager.GPS_PROVIDER;

    Intent intent = getIntent();
    String contactNo;
    contactNo = intent.getStringExtra("PhoneNumber");
    finalnumber = contactNo;
    String message;
    message = intent.getStringExtra("TextMessage");
    finalmessage = message;
    double coord1 = 0;
    coord1 = intent.getDoubleExtra("Coordinate1", coord1);
    finalc1 = coord1;
    double coord2 = 0;
    coord2 = intent.getDoubleExtra("Coordinate2", coord2);
    finalc2 = coord2;
    int seldis = 0;
    seldis = intent.getIntExtra("SelectedDistance", seldis);
    finalsd = seldis;

    LocationManager lm;
    double lat = finalc1;
    double long1 = finalc2;    //Defining Latitude & Longitude
    float radius=finalsd;                         //Defining Radius

           lm=(LocationManager) getSystemService(LOCATION_SERVICE);
           Intent i= new Intent();
           i.putExtra("PhoneNumber", contactNo);
           i.putExtra("TextMessage", message);
           PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);              
           lm.addProximityAlert(lat, long1, radius, -1, pi);
       }   
}

This is my Receiver.class (the broadcast receiver)

package com.example.drivetext;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.telephony.SmsManager;

public class Receiver extends BroadcastReceiver {

String finalnumber;
String finalmessage;

 @Override
 public void onReceive(Context arg0, Intent arg1) { 

     Intent intent4 = getIntent();
     String contactNo;
     contactNo = intent4.getStringExtra("PhoneNumber");
     finalnumber = contactNo;
     String message;
     message = intent4.getStringExtra("TextMessage");
     finalmessage = message;

  String k=LocationManager.KEY_PROXIMITY_ENTERING;
 // Key for determining whether user is leaving or entering 

  boolean state=arg1.getBooleanExtra(k, false);
  //Gives whether the user is entering or leaving in boolean form

  if(state){
   // Call the Notification Service or anything else that you would like to do here
   SmsManager smsManager = SmsManager.getDefault();
   String sendTo = finalnumber;
   String myMessage = finalmessage;
   smsManager.sendTextMessage(sendTo, null, myMessage, null, null);
   }

}
}

Upvotes: 1

Views: 4559

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

However, within my Receiver class getIntent() method is undefined.

getIntent() is method of Activity class. You can use arg1 which is a param @ public void onReceive(Context arg0, Intent arg1) {

String contactNo;
contactNo = arg1.getStringExtra("PhoneNumber");

You also need to send the broadcast sendBroadcast(intent);

http://developer.android.com/reference/android/content/ContextWrapper.html#sendBroadcast(android.content.Intent)

Upvotes: 6

Related Questions