【发布时间】:2015-05-24 19:21:20
【问题描述】:
我正在尝试实现一个使用接近警报并在进入和退出某些定义区域时发出警报的 android 简单代码
代码运行没有错误,但我想测试它是否适用于坐标(首先不在该区域,然后进入应该收到警报,然后退出并收到另一个警报)所以我用谷歌搜索了如何使用 telnet 给lat,long 但我发现的只是给出固定值
还有其他方法可以解决这个问题吗? PS:我正在使用android studio :)
编辑:我想出了如何更改坐标并按照教程进行接近测试...代码运行没有任何错误,但发出警报的意图似乎没有触发 这是我的代码: 主活动
public class MainActivity extends ActionBarActivity {
LocationManager lm;
double lat=32.001271,long1=35.950375; //Defining Latitude & Longitude
float radius=100; //Defining Radius
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm=(LocationManager) getSystemService(LOCATION_SERVICE);
Intent i= new Intent("com.example.hala.proximityalert"); //Custom Action
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
lm.addProximityAlert(lat, long1, radius, -1, pi);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ProximityReceiver
public class ProximityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
// The reciever gets the Context & the Intent that fired the broadcast as arg0 & agr1
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
Toast.makeText(arg0, "Welcome to my Area", Toast.LENGTH_LONG).show();
}else{
//Other custom Notification
Toast.makeText(arg0, "Thank you for visiting my Area,come back again !!", Toast.LENGTH_LONG).show();
}
}
}
所以如果有人知道它为什么不触发,我将非常感谢您的帮助
【问题讨论】:
标签: android coordinates telnet proximity