我正在尝试编写一个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);
}
}
答案 0 :(得分:0)
您应该按照以下方式在AndroidManifest中注册您的接收器
<receiver android:name="MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
您的广播接收器课程在哪里?您必须通过扩展BroadcastReceiver
来创建接收器类MyReceiver extends BroadcastReceiver
Here是关于如何在android中使用接收器的教程