【发布时间】:2015-09-13 19:12:09
【问题描述】:
我正在 Android 中开发 Beacon 应用程序。我想要一个功能,当应用程序进入信标区域时,即使应用程序未在 android 中运行,通知也应该出现在设备中关于“区域已进入”。当它退出时,“区域退出然后再次通知应该来。我不是当应用程序未在后台运行时,如何在顶部栏中显示通知。 此外,当我单击通知时,它应该将我带到信标屏幕。 请帮助我,我是新手。在此先感谢 这是我的代码
public class NotifyDemoActivity extends BaseActivity {
private static final String TAG =NotifyDemoActivity.class.getSimpleName();
private static final int NOTIFICATION_ID = 123;
private BeaconManager beaconManager;
private NotificationManager notificationManager;
private Region region;
@Override protected int getLayoutResId() {
return R.layout.notify_demo;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Beacon beacon = getIntent().getParcelableExtra(ListBeaconsActivity.EXTRAS_BEACON);
region = new Region("regionId", beacon.getProximityUUID(), beacon.getMajor(), beacon.getMinor());
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
beaconManager = new BeaconManager(this);
// Default values are 5s of scanning and 25s of waiting time to save CPU cycles.
// In order for this demo to be more responsive and immediate we lower down those values.
beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(3), 0);
beaconManager.setMonitoringListener(new MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
postNotification("Entered region");
Log.d(TAG, "entered");
}
@Override
public void onExitedRegion(Region region) {
postNotification("Exited region");
Log.d(TAG, "exited");
}
});
}
@Override
protected void onResume() {
super.onResume();
notificationManager.cancel(NOTIFICATION_ID);
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
try {
beaconManager.startMonitoring(region);
} catch (RemoteException e) {
Log.d(TAG, "Error while starting monitoring");
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
/* notificationManager.cancel(NOTIFICATION_ID);*/
Log.d(TAG, "Beacons monitoring service destroyed");
Toast.makeText(this, "Beacons monitoring service done", Toast.LENGTH_SHORT).show();
Notification noti = new Notification.Builder(NotifyDemoActivity.this)
.setContentTitle("Stopped")
.setContentText("See you!")
.setSmallIcon(R.drawable.variance_new_logo)
.build();
/*
beaconManager.disconnect();
super.onDestroy();
*/}
private void postNotification(String msg) {
Intent notifyIntent = new Intent(NotifyDemoActivity.this,DistanceBeaconActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(
NotifyDemoActivity.this,
0,
new Intent[]{notifyIntent},
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(NotifyDemoActivity.this)
.setSmallIcon(R.drawable.beacon_gray)
.setContentTitle("Beacon Found")
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(NOTIFICATION_ID, notification);
;
TextView statusTextView = (TextView) findViewById(R.id.status);
statusTextView.setText(msg);
}
}
【问题讨论】:
标签: android monitoring android-notifications estimote android-ibeacon