Android的GPS接近提醒不工作工作、Android、GPS

2023-09-05 03:18:37 作者:[勋宝]

我试图建立最基本的接近警戒,但似乎没有发生。我是相当新的Andr​​oid的编程,所以请让我知道我做错了还是我缺少什么。我从一些源$ C ​​$ CS这里拿出来和的这个。这是我的code:

 包com.example.proximityalert;

进口android.location.LocationManager;
进口android.os.Bundle;
进口android.app.Activity;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;

公共类MainActivity延伸活动{

    私有静态最后弦乐PROX_ALERT_INTENT =com.example.proximityalert;
    私人IntentReceiver locationReminderReceiver;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);


         意向意图=新的意图(PROX_ALERT_INTENT);
         PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,意向,PendingIntent.FLAG_ONE_SHOT);

         LocationManager locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
         locationManager.addProximityAlert(55.586301,13.045417,200,-1,pendingIntent);

         this.locationReminderReceiver =新IntentReceiver();

         最后IntentFilter的过滤器=新的IntentFilter(PROX_ALERT_INTENT);
         this.registerReceiver(this.locationReminderReceiver,过滤器);

    }
}
 

和接收器

 包com.example.proximityalert;

进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.location.LocationManager;
进口android.util.Log;
进口android.widget.Toast;

公共类IntentReceiver扩展的BroadcastReceiver {

// @燮pressWarnings(德precation)
       @覆盖
       公共无效的onReceive(上下文的背景下,意图意图){


          字符串键= LocationManager.KEY_PROXIMITY_ENTERING;

          布尔进入= intent.getBooleanExtra(键,FALSE);

          如果(进入){
              Toast.makeText(上下文中,LocationReminderReceiver输入,Toast.LENGTH_SHORT).show();
              Log.i(LocationReminderReceiver,输入);
          } 其他 {
              Toast.makeText(上下文中,LocationReminderReceiver退出,Toast.LENGTH_SHORT).show();
              Log.i(LocationReminderReceiver,退出);
          }
       }
    }
 

解决方案 中关村在线 Android Wear首次更新

这是我用得到接近警报,您的欢迎使用它的服务,它并不需要在清单中,就在服务reciever

 进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.app.Service;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.database.Cursor;
进口android.graphics.Color;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.os.IBinder;
进口android.util.Log;
进口android.widget.Toast;


公共类ProximityService延伸服务{
字符串proximitysd =com.apps.ProximityService;
INT N = 0;
私人BroadcastReceiver的mybroadcast;
私人LocationManager locationManager;
MyLocationListener locationListenerp;
公共ProximityService(){


}

@覆盖
公众的IBinder onBind(意向意图){
    // TODO自动生成方法存根
    返回null;
}
@覆盖
公共无效的onCreate(){
    mybroadcast =新ProximityIntentReceiver();
    locationManager =(LocationManager)
            getSystemService(Context.LOCATION_SERVICE);





    双纬度;
    双LNG;
    浮半径= 50F;
    长的到期= -1;
     MyDBAdapter DB =新MyDBAdapter(本);
        光标光标;
        db.read();
        光标= db.getAllEntries();
        布尔去= cursor.moveToFirst();
        而(cursor.isAfterLast()!= TRUE){
            纬度= cursor.getInt(MyDBAdapter.LATITUDE_COLUMN)/ 1E6;
            LNG = cursor.getInt(MyDBAdapter.LONGITUDE_COLUMN)/ 1E6;
            字符串内容= cursor.getString(MyDBAdapter.ICON_COLUMN);
            字符串如何= cursor.getString(MyDBAdapter.FISH_COLUMN);
            字符串proximitys =com.apps.ProximityService+ N;
            IntentFilter的过滤器=新的IntentFilter(proximitys);
            registerReceiver(mybroadcast,过滤器);

            意向意图=新的意图(proximitys);

            intent.putExtra(警告,什么);
            intent.putExtra(类,如何);
            PendingIntent proximityIntent = PendingIntent.getBroadcast(本,N,意图,PendingIntent.FLAG_CANCEL_CURRENT);
            locationManager.addProximityAlert(纬度,经度,半径,到期proximityIntent);
            // sendBroadcast(新意向(意向));

            ñ++;
            cursor.moveToNext();
        }
        db.close();
        cursor.close();
}

@覆盖
公共无效的onDestroy(){
    Toast.makeText(这一点,接近服务已停止,Toast.LENGTH_LONG).show();
    尝试{
        unregisterReceiver(mybroadcast);
    }赶上(抛出:IllegalArgumentException E){
        Log.d(reciever,e.toString());
    }


}
@覆盖
公共无效ONSTART(意向意图,诠释startid){
    Toast.makeText(这一点,接近服务启动,Toast.LENGTH_LONG).show();
    // IntentFilter的过滤器=新的IntentFilter(proximitys);
    // registerReceiver(mybroadcast,过滤器);


}
公共类ProximityIntentReceiver扩展的BroadcastReceiver {
     私有静态最终诠释NOTIFICATION_ID = 1000;
    @覆盖
    公共无效的onReceive(背景为arg0,意图ARG1){
        字符串键= LocationManager.KEY_PROXIMITY_ENTERING;

        布尔进入= arg1.getBooleanExtra(键,FALSE);
        字符串这里= arg1.getExtras()的getString(警告)。
        。字符串幸福= arg1.getExtras()的getString(类型);



         NotificationManager notificationManager =
                    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

                PendingIntent pendingIntent = PendingIntent.getActivity(为arg0,0,ARG1,0);

                通知通知= createNotification();

                notification.setLatestEventInfo(为arg0,
                    !进入接近,您正在接近+在这里+标志。,pendingIntent);

                notificationManager.notify(NOTIFICATION_ID,通知);


            }

            民营通知createNotification(){
                通知通知=新的通知();

                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;


                返回通知;
            }
        //使行动



}
 公共类MyLocationListener实现LocationListener的{
        公共无效onLocationChanged(位置定位){
            Toast.makeText(getApplicationContext(),我在这里,Toast.LENGTH_LONG).show();
        }

        公共无效onProviderDisabled(String s)将{
        }
        公共无效onProviderEnabled(String s)将{
        }
        @覆盖
        公共无效onStatusChanged(字符串为arg0,INT ARG1,捆绑ARG2){
            // TODO自动生成方法存根

        }
    }

}
 

您必须更换数据库查询和一些code,以满足您的应用程序,这是一个多接近警报这就是为什么有n的增量。

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

    }
}

and the Receiver

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

        }
    }

}

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.