getIntent in broadcast receiver undefined android

时间:2014-05-13 05:10:59

标签: android broadcastreceiver android-pendingintent

在我的应用程序中,我有一个未决的意图,当广播满足其要求时,广播接收器会监听。这个广播接收器然后执行我想要它做的事情,但是我需要从一个活动添加额外的活动到另一个活动,我使用intent.putExtra来执行此操作。 `

但是,在我的Receiver类中,getIntent()方法未定义。将它与待定意图一起使用时如何工作,因为它与我所有其他意图一起使用。

这是我的targetdistance.class :(设置广播和待定意图)

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);
       }   
}

这是我的Receiver.class(广播接收器)

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);
   }

}
}

1 个答案:

答案 0 :(得分:5)

  

但是,在我的Receiver类中,getIntent()方法未定义。

getIntent()是Activity类的方法。您可以使用arg1这是一个@ public void onReceive(Context arg0, Intent arg1) {

的参数
String contactNo;
contactNo = arg1.getStringExtra("PhoneNumber");

您还需要发送广播sendBroadcast(intent);

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