【发布时间】: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