将iBeacon主ID从Java类传递到Android Studio中的活动
我试图以字符串形式从iBeacon获取主ID,并将其传递给名为“LoginActivity.java”的活动,这样我就可以通过截图将其与登录信息用户名和密码一起传递给我的PHP脚本,脚本将首先检查信标值是否为空,如果是,则返回一个错误,即信标不在范围内,因此他们无法登录
到目前为止,我已经获得了主ID,并将其转换为字符串,但在创建intent“Cannot resolve constructor”时出现了一个错误。我用下面的<<<<ERROR
标记了我得到错误的那一行。(快到尽头了)
package com.mcrlogs.pp.test;
/**
* Created by myuser on 15/01/2017.
*/
import 安卓.app.Application;
import 安卓.app.Notification;
import 安卓.app.NotificationManager;
import 安卓.app.PendingIntent;
import 安卓.bluetooth.BluetoothClass;
import 安卓.content.Context;
import 安卓.content.Intent;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import java.util.List;
import java.util.UUID;
public class BeaconChecker extends Application {
private BeaconManager beaconManager;
public void showNotification(String title, String message) {
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0,
new Intent[] { notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.security)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
@Override
public void onCreate() {
super.onCreate();
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startMonitoring(new Region(
"monitored region",
UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"),
null, null));
}
});
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> list) {
String iBeaconID = convertIBeaconIDToString(list.get(0).getMajor());
System.out.println(iBeaconID);
showNotification(
"MCR Beacon Detected!",
"Login enabled.");
}
private String convertIBeaconIDToString (int major) {
String iBeaconID = "";
iBeaconID = iBeaconID.concat(Integer.toString(major));
return iBeaconID;
Intent i = new Intent(this, LoginActivity.class); <<<<ERROR
i.putExtra("iBeaconID",iBeaconID);
}
@Override
public void onExitedRegion(Region region) {
// could add an "exit" notification too if you want (-:
}
});
}
}
# 1 楼答案
尝试改变:
致:
这澄清了您所指的
this
是满足Intent
构造函数要求的Application
类