Android GPS 接近警报不起作用

编程入门 行业动态 更新时间:2024-10-27 06:19:12
本文介绍了Android GPS 接近警报不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试设置最基本的接近警报,但似乎没有任何反应.我对 Android 编程相当陌生,所以请让我知道我做错了什么或者我错过了什么.我从这里的一些源代码和 这个.这是我的代码:

I'm trying to set up the most basic proximity alert, but nothing seems to happen. I'm fairly new to Android programming, so please let me know what I'm doing wrong or what am I missing. I got inspired from some source codes out here and this one. Here's my code:

package com.example.proximityalert; import android.location.LocationManager; import android.os.Bundle; import android.app.Activity; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; public class MainActivity extends Activity { private static final String PROX_ALERT_INTENT = "com.example.proximityalert"; private IntentReceiver locationReminderReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(PROX_ALERT_INTENT); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT); LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); locationManager.addProximityAlert(55.586301,13.045417, 200, -1, pendingIntent); this.locationReminderReceiver = new IntentReceiver(); final IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); this.registerReceiver(this.locationReminderReceiver, filter); } }

和接收者

package com.example.proximityalert; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.location.LocationManager; import android.util.Log; import android.widget.Toast; public class IntentReceiver extends BroadcastReceiver{ // @SuppressWarnings("deprecation") @Override public void onReceive(Context context, Intent intent) { String key = LocationManager.KEY_PROXIMITY_ENTERING; Boolean entering = intent.getBooleanExtra(key, false); if (entering) { Toast.makeText(context, "LocationReminderReceiver entering", Toast.LENGTH_SHORT).show(); Log.i("LocationReminderReceiver", "entering"); } else { Toast.makeText(context, "LocationReminderReceiver exiting", Toast.LENGTH_SHORT).show(); Log.i("LocationReminderReceiver", "exiting"); } } }

推荐答案

这是我用来获取接近警报的服务,欢迎您使用它,它不需要清单中的接收者,只需要服务

This is the service that i use to get proximity alerts, your welcome to use it, it doesnt need the reciever in the manifest, just the service

import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.graphics.Color; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class ProximityService extends Service{ String proximitysd = "com.apps.ProximityService"; int n = 0; private BroadcastReceiver mybroadcast; private LocationManager locationManager; MyLocationListener locationListenerp; public ProximityService() { } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { mybroadcast = new ProximityIntentReceiver(); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); double lat; double lng; float radius = 50f; long expiration = -1; MyDBAdapter db = new MyDBAdapter(this); Cursor cursor; db.read(); cursor = db.getAllEntries(); boolean go = cursor.moveToFirst(); while(cursor.isAfterLast() != true){ lat = cursor.getInt(MyDBAdapter.LATITUDE_COLUMN)/1E6; lng = cursor.getInt(MyDBAdapter.LONGITUDE_COLUMN)/1E6; String what = cursor.getString(MyDBAdapter.ICON_COLUMN); String how = cursor.getString(MyDBAdapter.FISH_COLUMN); String proximitys = "com.apps.ProximityService" + n; IntentFilter filter = new IntentFilter(proximitys); registerReceiver(mybroadcast, filter ); Intent intent = new Intent(proximitys); intent.putExtra("alert", what); intent.putExtra("type", how); PendingIntent proximityIntent = PendingIntent.getBroadcast(this, n, intent, PendingIntent.FLAG_CANCEL_CURRENT); locationManager.addProximityAlert(lat, lng, radius, expiration, proximityIntent); //sendBroadcast(new Intent(intent)); n++; cursor.moveToNext(); } db.close(); cursor.close(); } @Override public void onDestroy() { Toast.makeText(this, "Proximity Service Stopped", Toast.LENGTH_LONG).show(); try{ unregisterReceiver(mybroadcast); }catch(IllegalArgumentException e){ Log.d("reciever",e.toString()); } } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "Proximity Service Started", Toast.LENGTH_LONG).show(); //IntentFilter filter = new IntentFilter(proximitys); //registerReceiver(mybroadcast,filter); } public class ProximityIntentReceiver extends BroadcastReceiver{ private static final int NOTIFICATION_ID = 1000; @Override public void onReceive(Context arg0, Intent arg1) { String key = LocationManager.KEY_PROXIMITY_ENTERING; Boolean entering = arg1.getBooleanExtra(key, false); String here = arg1.getExtras().getString("alert"); String happy = arg1.getExtras().getString("type"); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent pendingIntent = PendingIntent.getActivity(arg0, 0, arg1, 0); Notification notification = createNotification(); notification.setLatestEventInfo(arg0, "Entering Proximity!", "You are approaching a " + here + " marker.", pendingIntent); notificationManager.notify(NOTIFICATION_ID, notification); } private Notification createNotification() { Notification notification = new Notification(); notification.icon = R.drawable.icon; notification.when = System.currentTimeMillis(); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.flags |= Notification.FLAG_SHOW_LIGHTS; notification.defaults |= Notification.DEFAULT_VIBRATE; notification.defaults |= Notification.DEFAULT_LIGHTS; notification.ledARGB = Color.WHITE; notification.ledOnMS = 1500; notification.ledOffMS = 1500; return notification; } //make actions } public class MyLocationListener implements LocationListener { public void onLocationChanged(Location location) { Toast.makeText(getApplicationContext(), "I was here", Toast.LENGTH_LONG).show(); } public void onProviderDisabled(String s) { } public void onProviderEnabled(String s) { } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } } }

您必须替换数据库查询和一些代码以适应您的应用程序,这是针对多个邻近警报,这就是 n 增加的原因.

you would have to replace the database query and some of the code to fit your app, this is for multiple proximity alerts thats why there is the increment of n.

更多推荐

Android GPS 接近警报不起作用

本文发布于:2023-11-27 20:30:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   不起作用   Android   GPS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!