簡體   English   中英

接近警報不起作用

[英]Proximity alert not working

我知道有很多這樣的問題,但是似乎沒有一個問題適合我,而且由於我對正在發生的事情沒有任何反饋,這讓我有點沮喪。 這是我的代碼

package com.example;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.media.RingtoneManager;
import android.net.Uri;

import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Spinner;

public class InformMe extends Activity implements OnClickListener{

private static final long EXPIRATION = -1;
private static final String PROXIMITY_ALERT = "com.example.PROXIMITY_ALERT";

private LocationManager locationManager;
private AttractionNearReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_informme);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    View btnAdd = findViewById(R.id.btn_set_inform);
    btnAdd.setOnClickListener(this);

    receiver = new AttractionNearReceiver();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    float radius = Float.parseFloat(((EditText)findViewById(R.id.edit_radius)).getText().toString());

    addProximityAlert(41.8723889, 12.48018, radius);

}

private void addProximityAlert(double latitude, double longitude, float radius)
{
    Intent intent = new Intent(PROXIMITY_ALERT);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    locationManager.addProximityAlert(latitude, longitude, radius, EXPIRATION, pendingIntent);
}

@Override
protected void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter(PROXIMITY_ALERT); 
    registerReceiver(receiver, filter);
}

@Override
protected void onPause() {
    super.onPause();

    unregisterReceiver(receiver);
}
}

和接收器

package com.example;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

public class AttractionNearReceiver extends BroadcastReceiver{

public static final int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    long[] vibratePatern = {0, 300, 200, 300, 200, 300};

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent notificationIntent = new Intent(context, SeeAttractionOrComment.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                                            .setTicker("near you")
                                            .setContentText("Added by")
                                            .setContentTitle("beach")
                                            .setSmallIcon(R.drawable.beach) 
                                            .setAutoCancel(true)
                                            .setContentIntent(pendingIntent)
                                            .setSound(alarmSound)
                                            .setVibrate(vibratePatern);

    NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, notificationBuilder.build());
}

}

沒有錯誤,沒有例外,什么都沒有。 我已經在仿真器和實際設備上進行了嘗試,但是沒有通知。

有人知道為什么嗎? 謝謝

正如@Gabe Sechan所說,您指向的是您可能不希望的地方。

LocationManager.addProximityAlert需要緯度和經度才能正常工作。 查看android文檔:

http://developer.android.com/reference/android/location/LocationManager.html

public void addProximityAlert (double latitude, double longitude, float radius, long expiration, PendingIntent intent)

注意:針對由位置(緯度,經度)和給定半徑指定的位置設置接近警報。

您可以使用getLastKnowLocation通過所需的提供程序獲取位置,然后使用獲取的值調用addProximityAlert。

public Location getLastKnownLocation (String provider)

返回一個位置,該位置指示從給定提供者獲得的最新已知位置修復中的數據。

像這樣:

Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

addProximityAlert(lastKnownLocationGPS.getLatitude(), lastKnownLocationGPS.getLongitude(), radius);

記住要驗證是否為空。

您正在添加位置(0,0)的接近警報。 那將在大西洋。 假設您不在游輪上,那可能不是您想要的。

編輯:這是我過去用來添加警報的一些工作代碼。 隨時適應它。

private int requestID = 0;

public void addProximityAlert(double latitude, double longitude, int radius, String category, Object data, ProximityListener listener) {
    Intent intent = new Intent(PROX_INTENT_NAME);
    intent.putExtra("latitude", latitude);
    intent.putExtra("longitude", longitude);

    LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

    //Request id parameter is not documented, but allows us to have multiple alerts outstanding so long as they are unique
    PendingIntent proximityIntent = PendingIntent.getBroadcast(mContext, requestID++, intent, 0);
    mAlerts.add(new AlertData(latitude, longitude, radius, category, data, listener, proximityIntent));
    locationManager.addProximityAlert(latitude, longitude, radius, 
                                       -1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration 
                                       proximityIntent);
    IntentFilter filter = new IntentFilter(PROX_INTENT_NAME);  
    mContext.registerReceiver(proximityAlertReceiver, filter);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM