2013-05-07 58 views
3

我正在嘗試設置最基本的接近警報,但似乎沒有發生。我對Android編程相當陌生,所以請讓我知道我做錯了什麼或者我錯過了什麼。我從這裏得到了一些源代碼的啓發,並且this one。這裏是我的代碼:Android GPS接近警報不起作用

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"); 
      } 
     } 
    } 
+0

嘗試將您的意圖和pendingintent移動到您註冊後收件人 – JRowan 2013-05-07 21:47:55

+0

我已將其更改爲此,並且仍然沒有任何內容。 'this.locationReminderReceiver = new IntentReceiver(); \t \t \t \t IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); \t \t this.registerReceiver(this.locationReminderReceiver,filter); \t \t \t \t Intent intent = new Intent(PROX_ALERT_INTENT); \t \t PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,PendingIntent.FLAG_ONE_SHOT); \t \t LocationManager locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); \t \t locationManager.addProximityAlert(55.586301,13.045417,200,-1的PendingIntent);' – 2013-05-08 06:22:39

+0

礦山工程和唯一性差異是我PendingIntent.FLAG_CANCEL_CURRENT代替PendingIntent.FLAG_ONE_SHOT和IM運行它的服務 – JRowan 2013-05-08 11:39:15

回答

1

這是我用它來獲得接近警報的服務,您的歡迎使用它,它不需要reciever在清單,只是服務

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的增量。

+0

所以我刪除了與db相關的東西和額外信息,設置了座標,在清單中定義了服務,並且仍然什麼都沒發生。它顯示服務開始的敬酒,但沒有通知或任何接近。我也嘗試過在手機上,並設置與騙子協調..我絕對不知道爲什麼它不起作用 ' – 2013-05-08 19:03:27

+0

我不認爲它在模擬器中的作品,它的工作原理,我知道它在市場上的權利現在 – JRowan 2013-05-08 19:39:27

+0

我發現它爲什麼不工作。當一個接近警報被定義時,它被設置爲(lat,lng),但是在DDMS中它們是相反的:)) – 2013-05-08 21:48:18