在 Android Studio 中将 iBeacon Major ID 从 Java Class 传递到 activity
Pass an iBeacon Major ID from Java Class to activity in Android Studio
我正在尝试从 iBeacon 获取主要 ID 作为字符串并将其传递给名为 "LoginActivity.java" 的 activity,以便我可以将其传递给我的 PHP通过 Volley POST 的脚本以及登录信息用户名和密码,脚本将首先检查信标值是否为 NULL,如果是 return 一个错误,表明信标不在范围内,因此他们无法登录。
到目前为止,我已经获得了 Major ID 并将其转换为字符串,但在创建意图时出现错误 "Cannot resolve constructor"。我在下面用 <<<<ERROR
标记了出现错误的行。 (接近尾声)
package com.mcrlogs.pp.test;
/**
* Created by myuser on 15/01/2017.
*/
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.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 (-:
}
});
}
}
尝试更改:
Intent i = new Intent(this, LoginActivity.class);
收件人:
Intent i = new Intent(BeaconChecker.this, LoginActivity.class);
这说明你所指的this
是Application
class满足Intent
.
构造函数的要求
我正在尝试从 iBeacon 获取主要 ID 作为字符串并将其传递给名为 "LoginActivity.java" 的 activity,以便我可以将其传递给我的 PHP通过 Volley POST 的脚本以及登录信息用户名和密码,脚本将首先检查信标值是否为 NULL,如果是 return 一个错误,表明信标不在范围内,因此他们无法登录。
到目前为止,我已经获得了 Major ID 并将其转换为字符串,但在创建意图时出现错误 "Cannot resolve constructor"。我在下面用 <<<<ERROR
标记了出现错误的行。 (接近尾声)
package com.mcrlogs.pp.test;
/**
* Created by myuser on 15/01/2017.
*/
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.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 (-:
}
});
}
}
尝试更改:
Intent i = new Intent(this, LoginActivity.class);
收件人:
Intent i = new Intent(BeaconChecker.this, LoginActivity.class);
这说明你所指的this
是Application
class满足Intent
.