-1

我正在尝试编写一段 GPS 接近传感器代码,当用户输入半径时将发送文本。这是我到目前为止所拥有的,但没有任何反应。我没有在清单中声明任何关于接收器的信息,因为当我这样做时,我被告知这不是必需的。

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;
import android.telephony.SmsManager;
import android.util.Log;

public class targetdistance extends Activity {

double finalc1;
double finalc2;
int finalsd;
String finalmessage;
String finalnumber;
private LocationManager locationManager;

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

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    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;

    double lat = finalc1;
    double long1 = finalc2;    //Defining Latitude & Longitude
    float radius=finalsd;                         //Defining Radius
    long expiration = -1;

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Intent intent1 = new Intent(); //proximityIntentAction

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
    locationManager.addProximityAlert(lat, long1, radius, expiration, pendingIntent);

}

public void onReceive(Context context, Intent intent)
{
    Log.v("SomeTag","Proximity Alert Intent Received");
    String direction = LocationManager.KEY_PROXIMITY_ENTERING;

    SmsManager smsManager = SmsManager.getDefault();
    String sendTo = finalnumber;
    String myMessage = finalmessage;
    smsManager.sendTextMessage(sendTo, null, myMessage, null, null);
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_NO_CREATE);
    locationManager.removeProximityAlert(pendingIntent);
}

}

4

1 回答 1

0

您应该通过以下方式在 AndroidManifest 中注册您的接收者

 <receiver android:name="MyScheduleReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
    </receiver>

你的广播接收器类在哪里?您必须通过扩展 BroadcastReceiver 创建一个接收器类

MyReceiver extends BroadcastReceiver

是关于如何在android中使用接收器的教程

于 2014-05-13T07:16:49.803 回答