我知道有很多这样的问题,但它们似乎都不适合我,而且它有点令人沮丧,因为我没有对正在发生的事情的反馈。这是我的代码
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());
}
}
没有错误,没有例外,没有任何错误。我已尝试过模拟器和真实设备,但没有通知。
有谁知道为什么? 感谢
答案 0 :(得分:1)
正如@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);
请记住验证null返回。
答案 1 :(得分:0)
您正在为该位置添加邻近警报(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);
}