有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Eddystone信标在Android状态栏中显示通知

我已经成功创建了一个应用程序(感谢DavidYoung!)它在后台监控信标,然后在后台打开应用程序

现在,我希望我的应用程序首先在状态栏中提示一个通知,比如“我检测到一个信标!你想打开应用程序吗?”。然后,用户会点击通知打开应用程序,或关闭应用程序并忽略通知

我在stack overflow上搜索过类似的内容,但没有找到与信标相关的内容。我确实找到了这个关于添加StatusBar通知的页面,但我没有太大成功

尤其是在我的BeaconReferenceApplication.javaMonitoringActivity.java文件中。我想我把代码放在了正确的位置(在didEnterRegion之后),但是我有一些未解析的类,比如notificationButtonsetLatestEventInfo等等。有人能帮忙吗?提前谢谢

BeaconReferenceApplication。爪哇:

import 安卓.app.Application;
import 安卓.app.Notification;
import 安卓.app.NotificationManager;
import 安卓.app.PendingIntent;
import 安卓.app.TaskStackBuilder;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.support.v4.app.NotificationCompat;
import 安卓.util.Log;

import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.powersave.BackgroundPowerSaver;
import org.altbeacon.beacon.startup.RegionBootstrap;
import org.altbeacon.beacon.startup.BootstrapNotifier;

public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
    private static final String TAG = "BeaconReferenceApp";
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;
    private boolean haveDetectedBeaconsSinceBoot = false;
    private MonitoringActivity monitoringActivity = null;


    public void onCreate() {
        super.onCreate();
        BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);

        // By default the AndroidBeaconLibrary will only find AltBeacons.  If you wish to make it
        // find a different type of beacon, you must specify the byte layout for that beacon's
        // advertisement with a line like below.  The example shows how to find a beacon with the
        // same byte layout as AltBeacon but with a beaconTypeCode of 0xaabb.  To find the proper
        // layout expression for other beacon types, do a web search for "setBeaconLayout"
        // including the quotes.
        //
        beaconManager.getBeaconParsers().clear();
        beaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));

        Log.d(TAG, "setting up background monitoring for beacons and power saving");
        // wake up the app when a beacon is seen
        Region region = new Region("backgroundRegion", Identifier.parse("2F234454F4911BA9FFA6"), null, null);
        regionBootstrap = new RegionBootstrap(this, region);

        // simply constructing this class and holding a reference to it in your custom Application
        // class will automatically cause the BeaconLibrary to save battery whenever the application
        // is not visible.  This reduces bluetooth power usage by about 60%
        backgroundPowerSaver = new BackgroundPowerSaver(this);

        // If you wish to test beacon detection in the Android Emulator, you can use code like this:
        // BeaconManager.setBeaconSimulator(new TimedBeaconSimulator() );
        // ((TimedBeaconSimulator) BeaconManager.getBeaconSimulator()).createTimedSimulatedBeacons();
    }

    @Override
    public void didEnterRegion(Region arg0) {
        // In this example, this class sends a notification to the user whenever a Beacon
        // matching a Region (defined above) are first seen.
        Log.d(TAG, "did enter region.");
        if (!haveDetectedBeaconsSinceBoot) {
            Log.d(TAG, "sending notification to StatusBar");
            @SuppressWarnings("deprecation")
            private void Notify(String notificationTitle, String notificationMessage) {
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                @SuppressWarnings("deprecation")
                Notification notification = new Notification(R.mipmap.ic_launcher,
                        "New Message", System.currentTimeMillis());

                Intent notificationIntent = new Intent(this, MonitoringActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                        notificationIntent, 0);

                notification.setLatestEventInfo(MonitoringActivity.this, notificationTitle,
                        notificationMessage, pendingIntent);
                notificationManager.notify(9999, notification);
            }

        }
        } else {
            if (monitoringActivity != null) {
                Log.d(TAG, "auto launching MainActivity");

                // The very first time since boot that we detect an beacon, we launch the
                // MainActivity
                Intent intent = new Intent(this, MonitoringActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                // Important:  make sure to add 安卓:launchMode="singleInstance" in the manifest
                // to keep multiple copies of this activity from getting created if the user has
                // already manually launched the app.
                this.startActivity(intent);
                haveDetectedBeaconsSinceBoot = true;
            }
        }
    }

    @Override
    public void didExitRegion(Region region) {
        if (monitoringActivity != null) {
            Log.d(TAG,"I no longer see a beacon.");
        }
    }

    @Override
    public void didDetermineStateForRegion(int state, Region region) {
        if (monitoringActivity != null) {
            Log.d(TAG,"I have just switched from seeing/not seeing beacons: " + state);
        }
    }

    private void sendNotification() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setContentTitle("Beacon Reference Application")
                        .setContentText("An beacon is nearby.")
                        .setSmallIcon(R.mipmap.ic_launcher);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(new Intent(this, MonitoringActivity.class));
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        builder.setContentIntent(resultPendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());
    }

    public void setMonitoringActivity(MonitoringActivity activity) {
        this.monitoringActivity = activity;
    }

}

监控活动。爪哇:

import 安卓.Manifest;
import 安卓.annotation.SuppressLint;
import 安卓.annotation.TargetApi;

import 安卓.content.pm.PackageManager;
import 安卓.graphics.Bitmap;
import 安卓.os.Build;
import 安卓.os.Bundle;

import 安卓.app.Activity;
import 安卓.app.AlertDialog;
import 安卓.content.DialogInterface;
import 安卓.content.Intent;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.webkit.WebSettings;
import 安卓.webkit.WebView;
import 安卓.webkit.WebViewClient;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.ProgressBar;

import org.altbeacon.beacon.BeaconManager;

public class MonitoringActivity extends Activity  {
    protected static final String TAG = "MonitoringActivity";
    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;

    private WebView mWebView;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitoring);

        // code for button notification
        Button notificationButton = (Button) findViewById(R.id.notificationButton);

        notificationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Notify("Title: Meeting with Business",
                        "Msg:Pittsburg 10:00 AM EST ");
            }
        });

        // code for button notification

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://communionchapelefca.org/edy-home");
        mWebView.setWebViewClient(new MyAppWebViewClient());

        verifyBluetooth();
        Log.d(TAG, "Application just launched");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Android M Permission check
            if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("This app needs location access");
                builder.setMessage("Please grant location access so this app can detect beacons in the background.");
                builder.setPositiveButton(安卓.R.string.ok, null);
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                    @TargetApi(23)
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                PERMISSION_REQUEST_COARSE_LOCATION);
                    }

                });
                builder.show();
            }
        }
    }

    private class HelloWebViewClient extends WebViewClient{


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url)
        {
            webView.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(view.GONE);
        }

    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_COARSE_LOCATION: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "coarse location permission granted");
                } else {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Functionality limited");
                    builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
                    builder.setPositiveButton(安卓.R.string.ok, null);
                    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                        @Override
                        public void onDismiss(DialogInterface dialog) {
                        }

                    });
                    builder.show();
                }
                return;
            }
        }
    }

    public void onRangingClicked(View view) {
        Intent myIntent = new Intent(this, RangingActivity.class);
        this.startActivity(myIntent);
    }

    @Override
    public void onResume() {
        super.onResume();
        ((BeaconReferenceApplication) this.getApplicationContext()).setMonitoringActivity(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        ((BeaconReferenceApplication) this.getApplicationContext()).setMonitoringActivity(null);
    }

    private void verifyBluetooth() {

        try {
            if (!BeaconManager.getInstanceForApplication(this).checkAvailability()) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Bluetooth not enabled");
                builder.setMessage("Please enable bluetooth in settings and restart this application.");
                builder.setPositiveButton(安卓.R.string.ok, null);
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        finish();
                        System.exit(0);
                    }
                });
                builder.show();
            }
        }
        catch (RuntimeException e) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Bluetooth LE not available");
            builder.setMessage("Sorry, this device does not support Bluetooth LE.");
            builder.setPositiveButton(安卓.R.string.ok, null);
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    finish();
                    System.exit(0);
                }

            });
            builder.show();

        }

    }



}

共 (1) 个答案

  1. # 1 楼答案

    因此,我能够通过使用Android Developers website for Notifications中的示例来解决我的问题。我使用了他们的示例代码,根据自己的使用进行了调整,然后进一步使用了它们。bigText让我的通知看起来很棒。感谢他们和戴维格杨让我的应用程序正常运行。谢谢

    我也不需要编辑我的监控活动。就像我之前发布的那样

    最终BeaconReferenceApplication。爪哇:

    import android.app.Application;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.TaskStackBuilder;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.app.NotificationCompat;
    import android.util.Log;
    import android.widget.RemoteViews;
    
    import org.altbeacon.beacon.BeaconManager;
    import org.altbeacon.beacon.BeaconParser;
    import org.altbeacon.beacon.Identifier;
    import org.altbeacon.beacon.Region;
    import org.altbeacon.beacon.powersave.BackgroundPowerSaver;
    import org.altbeacon.beacon.startup.RegionBootstrap;
    import org.altbeacon.beacon.startup.BootstrapNotifier;
    
    public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
        private static final String TAG = "BeaconReferenceApp";
        private RegionBootstrap regionBootstrap;
        private BackgroundPowerSaver backgroundPowerSaver;
        private boolean haveDetectedBeaconsSinceBoot = false;
        private MonitoringActivity monitoringActivity = null;
    
    
        public void onCreate() {
            super.onCreate();
            BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
    
            // By default the AndroidBeaconLibrary will only find AltBeacons.  If you wish to make it
            // find a different type of beacon, you must specify the byte layout for that beacon's
            // advertisement with a line like below.  The example shows how to find a beacon with the
            // same byte layout as AltBeacon but with a beaconTypeCode of 0xaabb.  To find the proper
            // layout expression for other beacon types, do a web search for "setBeaconLayout"
            // including the quotes.
            //
            beaconManager.getBeaconParsers().clear();
            beaconManager.getBeaconParsers().add(new BeaconParser().
                    setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
    
            Log.d(TAG, "setting up background monitoring for beacons and power saving");
            // wake up the app when a beacon is seen
            Region region = new Region("backgroundRegion", Identifier.parse("2F234454F4911BA9FFA6"), null, null);
            regionBootstrap = new RegionBootstrap(this, region);
    
            // simply constructing this class and holding a reference to it in your custom Application
            // class will automatically cause the BeaconLibrary to save battery whenever the application
            // is not visible.  This reduces bluetooth power usage by about 60%
            backgroundPowerSaver = new BackgroundPowerSaver(this);
    
            // If you wish to test beacon detection in the Android Emulator, you can use code like this:
            // BeaconManager.setBeaconSimulator(new TimedBeaconSimulator() );
            // ((TimedBeaconSimulator) BeaconManager.getBeaconSimulator()).createTimedSimulatedBeacons();
        }
    
        @Override
        public void didEnterRegion(Region arg0) {
            // In this example, this class sends a notification to the user whenever a Beacon
            // matching a Region (defined above) are first seen.
            Log.d(TAG, "did enter region.");
            if (!haveDetectedBeaconsSinceBoot) {
                Log.d(TAG, "sending notification to StatusBar");
                //begin code for notification
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                                .setSmallIcon(R.drawable.notification_icon)
                                .setContentTitle("Message from Communion Chapel")
                                .setContentText("Welcome! Thanks for coming!")
                                .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText("We noticed that you're here today, click here to open the app and get today's Sermon Notes and Bulletin."))
                                .setAutoCancel(true);
                ;
    
    // Creates an explicit intent for an Activity in your app
                Intent resultIntent = new Intent(this, MonitoringActivity.class);
    
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
                stackBuilder.addParentStack(MonitoringActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent =
                        stackBuilder.getPendingIntent(
                                0,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
                mNotificationManager.notify(123, mBuilder.build());
            }
        }
    
        @Override
        public void didExitRegion(Region region) {
            if (monitoringActivity != null) {
                Log.d(TAG,"I no longer see a beacon.");
            }
        }
    
        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            if (monitoringActivity != null) {
                Log.d(TAG,"I have just switched from seeing/not seeing beacons: " + state);
            }
        }
    
        private void sendNotification() {
            NotificationCompat.Builder builder =
                    new NotificationCompat.Builder(this)
                            .setContentTitle("Beacon Reference Application")
                            .setContentText("A beacon is nearby.")
                            .setSmallIcon(R.drawable.app_icon);
    
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addNextIntent(new Intent(this, MonitoringActivity.class));
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                            0,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
            builder.setContentIntent(resultPendingIntent);
            NotificationManager notificationManager =
                    (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(1, builder.build());
        }
    
        public void setMonitoringActivity(MonitoringActivity activity) {
            this.monitoringActivity = activity;
        }
    
    }