如何在Proximity警报中初始化通知

时间:2016-02-14 18:25:57

标签: android notifications geolocation location

尝试使用Proximity Alert通知用户何时通过此位置。我将resultIntent分配给Notification_Handler.class,它会创建通知,但是当用户靠近该位置时,Not not not push,如何在这种情况下推送通知?

//This is android App 
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    // resize map
    mapFragment = (SupportMapFragment) (getSupportFragmentManager()
            .findFragmentById(R.id.map));
    ViewGroup.LayoutParams params = mapFragment.getView().getLayoutParams();
    params.height = 1400;
    mapFragment.getView().setLayoutParams(params);
    //Notify if user near a location
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Intent resultIntent = new Intent(this, Notification_Handler.class);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0,          resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    lm.addProximityAlert(21.422, 39.826, 5, -1, pendIntent);}

编辑:这是通知处理程序类

    package app.historical_markers.historical_markers;


    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.NotificationCompat;
    import android.support.v4.app.TaskStackBuilder;
    import android.support.v7.app.ActionBarActivity;

    import app.historical_markers.historical_markers.Not_Signed_In.KSA_Map;


    public class Notification_Handler extends ActionBarActivity {
        final int notificationID = 1234;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setSmallIcon(R.drawable.icon2);
        mBuilder.setContentTitle("Notification Alert!!!");
        mBuilder.setContentText("Hi, You are near this Marker!");

        Intent resultIntent = new Intent(this, KSA_Map.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(KSA_Map.class);

// Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

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

// notificationID allows you to update the notification later on.
        mNotificationManager.notify(notificationID, mBuilder.build());

    }}

1 个答案:

答案 0 :(得分:0)

LocationManager.addProximityAlert()中半径参数的单位是米。您指定的值为5.这太小了。 GPS并不准确,你已经指定你的纬度/经度只有10米的精度。

使用更大的半径值,例如100.创建PendingIntent的代码是正确的。