android - 从 Proximity IntentReceiver 发送短信

标签 android

程序员。我正在做一个 Proximity Alert App,想知道如何从 ProximityIntentReceiver extends BroadcastReceiver 发送 SMS,即在进入特定半径时自动发送 SMS。

下面是我的 ProximityIntentReceiver 代码,我可以在哪里放置 SMS Activity ?

package jacojunga.retaildistributortrack;

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



public class ProximityIntentReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 1000;
String sender;
IntentFilter intentFilter;

@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");
        Toast.makeText(context,sender ,
                Toast.LENGTH_SHORT).show();
        NotificationManager notificationManager =
                                                                               (NotificationManager)                                                                                             context.getSystemService(Context.NOTIFICATION_SERVICE);

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

                Notification notification = createNotification();
                notification.setLatestEventInfo(context,     "Proximity Alert!", "You are near your point of interest.", pendingIntent);

                notificationManager.notify(NOTIFICATION_ID,  notification);
    } else {
        Log.d(getClass().getSimpleName(), "exiting");
       }

         }

        private Notification createNotification() {
    Notification notification = new Notification();

    notification.icon = R.drawable.pushpin;
    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;
       }

  }

谢谢

最佳答案

使用广播接收器发送短信的代码是

SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, body, null, null);

您还需要将其添加到您的 list 中

<uses-permission android:name="android.permission.SEND_SMS" />

当您收到接近警报时放置它。

关于android - 从 Proximity IntentReceiver 发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13572341/

相关文章:

Android ListView 问题

android - 在 Android 中使用 Content Provider 批量删除项目

android - 从 Android SD 卡恢复已删除的数据?

android - 与 facebook Messenger 分享文字?

android - RxJava api 调用列表中的每个项目并返回扩展项目列表

java - 为什么不将异常(exception)添加到签名中?

android - 在 RippleDrawable 中为 StateListDrawable 更改 TintList

Android - 操作系统版本统计

android - 在 SQLite 和 Android 中使用列作为选择参数

Android:如何在大型滚动 Canvas 上绘制简单图形