接近警报不会触发并通知

Posted

技术标签:

【中文标题】接近警报不会触发并通知【英文标题】:proximity alert does not fire and notify 【发布时间】:2013-08-10 08:08:46 【问题描述】:

有谁知道进入邻近半径时导致火灾警报不起作用的问题是什么?我已经为解决这个问题苦苦挣扎了一周,如果有人能帮助我解决这个问题或给我一些指导来完成它,我将不胜感激。

MainActivity.java

import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;


public class MainActivity extends Activity 

    private static final long POINT_RADIUS = 150; // in Meters
    private static final long PROX_ALERT_EXPIRATION = -1; // will not expire
    private static final String PROX_ALERT_INTENT = "com.example.myalert";
    private LocationManager locationManager;
    double latitude = 2.81202, longitude = 101.75989;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        Intent intent = new Intent(PROX_ALERT_INTENT);
        PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        locationManager.addProximityAlert(latitude, longitude, POINT_RADIUS, PROX_ALERT_EXPIRATION, proximityIntent); 
    


ProximityReceiver.java

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.util.Log;

public class ProximityReceiver extends BroadcastReceiver
    private static final int NOTIFICATION_ID = 1000;

    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context context, Intent intent) 
       String key = LocationManager.KEY_PROXIMITY_ENTERING;
       Boolean entering = intent.getBooleanExtra(key, false);
       if (entering) 
                  Log.d(getClass().getSimpleName(), "entering");
           else 
                  Log.d(getClass().getSimpleName(), "exiting");
           
           NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

           Intent notificationIntent = new Intent(context, MainActivity.class);
           PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
           Notification notification = createNotification();
           notification.setLatestEventInfo(context, "Proximity Alert!", "You are near your point of interest.", pendingIntent);

           notificationManager.notify(NOTIFICATION_ID, notification);
    

    private Notification createNotification() 
        Notification notification = new Notification();
        notification.icon = R.drawable.ic_launcher;
        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;
  

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myalert"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myalert.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
             android:name="ProximityReceiver">
        </activity>
    </application>

</manifest>

【问题讨论】:

【参考方案1】:

你没有在manifest中设置receiver,“ProximityReceiver”不是Activity而是receiver。 试试这样的。

 <receiver android:name="ProximityReceiver" >
        <intent-filter>
            <action android:name="com.example.myalert" />
        </intent-filter>
  </receiver>

干杯。

【讨论】:

以上是关于接近警报不会触发并通知的主要内容,如果未能解决你的问题,请参考以下文章

当应用程序处于打盹模式时,警报管理器触发的 Android 通知未触发

如何在 iOS 中触发通知时打开警报视图?

AlarmManager 在错误的时间触发警报

在 Android 上显示已保存的接近警报?

在 iOS8.3 上显示警报视图时,iOS 键盘通知不必要地触发

如何使用 WatchKit 触发通知