【问题标题】:Android GPS proximity alert not workingAndroid GPS 接近警报不起作用
【发布时间】:2015-04-03 01:31:14
【问题描述】:

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

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.locationReminderReceiver = new IntentReceiver(); IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); this.registerReceiver(this.locationReminderReceiver, filter); 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);
  • 我的作品,唯一的区别是我有 PendingIntent.FLAG_CANCEL_CURRENT 而不是 PendingIntent.FLAG_ONE_SHOT 并且我在服务中运行它
  • 能否请您发布整个源代码?也许我遗漏了一些东西。另外,您是否在清单中声明了接收者?
  • 也许你必须将你的接收者声明为广播接收者而不是意图接收者

标签: android gps alert proximity


【解决方案1】:

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

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 增加的原因。

【讨论】:

  • 所以我已经删除了与数据库相关的东西和附加功能,设置了坐标,在清单中定义了服务,但仍然没有任何反应。它显示了服务启动的 toast,但没有通知或任何接近的东西。我也在手机上试过,用 fakegps 设置坐标。我完全不知道为什么它不工作`
  • 我不认为它在模拟器中有效,它有效,我知道它现在在市场上有效
  • 我发现了它为什么不起作用。当定义接近警报时,它被设置(lat,lng),但在 DDMS 中它们是相反的 :)))
  • 很高兴你能用它,希望它对你有用:)
  • 我认为使用 FLAG_CANCEL_CURRENT 或 FLAG_ONE_SHOT 完全没有区别。应该有吗?但是,每次我进入半径和每次退出半径时,它们都会创建通知。我希望它在我第一次进入半径时只创建一个通知,而这一切。 (退出时没有通知,以后进入也没有通知,但我不想停止服务)
猜你喜欢
  • 1970-01-01
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多