【问题标题】:Intent Filter Pending Intent , Broadcast Reciever and Notification Manager意图过滤器Pendingintent、广播接收器和通知管理器
【发布时间】:2015-09-18 04:58:51
【问题描述】:

我正在努力理解这些概念的工作原理。

谁能给我解释一下这些概念?

这是我想看的代码

Intent intent = new Intent();
        intent.setAction("com.example.akshay.proximityalertexample2");
        PendingIntent intent1 = PendingIntent.getBroadcast(this, 0, intent, 0);
        locationManager.addProximityAlert(LAT, LONG, 200, -1, intent1);
        IntentFilter filter = new IntentFilter("com.example.akshay.proximityalertexample2");
        registerReceiver(new ProximityAlert(), filter);

广播类文件

package com.example.akshay.proximityalertexample2;

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.location.LocationManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Akshay on 9/18/2015.
 */
public class ProximityAlert extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Toast.makeText(context, key, Toast.LENGTH_SHORT).show();
        Boolean entering = intent.getBooleanExtra(key, false);

        if (entering) {
            Log.e(getClass().getSimpleName(), "entering");

        } else {
            Log.e(getClass().getSimpleName(), "exiting");
        }
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

        Notification notification = createNotification();

        PendingIntent pendingIntent = PendingIntent.getActivity(context , 0 , null , 0 );

        notification.setLatestEventInfo(context,"Proximity Alert!!","You Are Near the Point of intereste " ,pendingIntent);

    }

    private Notification createNotification() {
        Notification notification = new Notification();
        notification.when = System.currentTimeMillis();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

        notification.ledOffMS = 1500;
        notification.ledOnMS = 1500;
        return notification;

    }
}

请解释一下这些概念的工作原理.. 任何帮助都意味着我很多

非常感谢

【问题讨论】:

  • 你的理解是什么?先告诉我们
  • 对于意图和待定意图:stackoverflow.com/questions/2808796/…
  • 我知道 Intent 可以用来启动其他活动,将数据传递给它们。待定意图用于在特定时间或事件触发意图。广播接收器用于在发生某些事情时执行特定任务,例如当我们将飞行模式更改为打开或关闭时。我不知道意图过滤器。请逐步解释他们是如何工作的
  • 我可以用一个例子来详细说明:假设你必须在社交媒体上分享一张图片,那时你会使用意图过滤器,因为你不知道用户是否想在 Facebook 上分享这张图片或 twitter 或 Google+,如果您在用户按下分享按钮时使用意图过滤器,它将打开选项供他自己选择在哪里分享,这就是意图过滤器的作用。

标签: android android-intent notifications broadcastreceiver intentfilter


【解决方案1】:

这是一个非常古老的线程,但我认为为了我自己的学习和其他人到达这个线程的好处,我会试一试。

  1. 在第一个 sn-p 中,您将创建一个不执行任何操作的 Intent,并用一个pendingIntent 包装它,以便稍后由应用程序调用。意图过滤器用于让其他应用程序(在这种情况下主要是接收者)知道我们正在处理什么样的意图(这是一个糟糕的例子,个人会使用 PROX_ALERT_INTENT 之类的东西)。我们添加一个接近警报,并将意图注册到我们的 Receiver 类中。

  2. 在 Receiver 类中,我们首先定义布尔键,用于确定我们是否已进入接近警报半径。然后我们从广播的意图中获取密钥(它自动传递),然后我们检查用户是否确实进入了接近警报区域。 如果他有,我们创建一个附加了待处理意图的通知(在这种情况下,它什么都不做,例如可以启动一个新活动),然后启动通知。

请注意,此通知构建方法已弃用,现在必须使用构建器。

【讨论】:

    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多