【问题标题】:GPS Proximity alert not working androidGPS接近警报不起作用android
【发布时间】:2014-05-13 06:52:33
【问题描述】:

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

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

}

【问题讨论】:

  • 你必须在清单中添加接收者

标签: android android-intent broadcastreceiver android-pendingintent proximitysensor


【解决方案1】:

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

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

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

MyReceiver extends BroadcastReceiver

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多