即使应用程序退出不工作,onstart命令也可以运行服务

时间:2015-11-04 17:49:15

标签: android beacon

即使应用程序关闭,我也希望我的服务能够运行。所以我做了以下事情。

   public class MyApplication extends Application {


RSSPullService mService;
boolean mBound = false;

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
                                   IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        RSSPullService.LocalBinder binder = (RSSPullService.LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};
@Override
public void onCreate() {
    super.onCreate();
    Intent mServiceIntent = new Intent(this, RSSPullService.class);
 //   startService(mServiceIntent);


    bindService(mServiceIntent, mConnection, Context.BIND_AUTO_CREATE);


}}

这是我的服务类,我在其中实现了onstartcommand函数,遗憾的是它不起作用。

  public class RSSPullService extends Service {
private BeaconManager beaconManager;
private BeaconManager beaconManager2;
private BeaconManager beaconManager3;

// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();

/**
 * Class used for the client Binder.  Because we know this service always
 * runs in the same process as its clients, we don't need to deal with IPC.
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    System.out.println("start sticky");
    Log.i("LocalService", "Received start id " + startId + ": " + intent);
    return START_STICKY;
}
public class LocalBinder extends Binder {
    RSSPullService getService() {
        // Return this instance of LocalService so clients can call public methods
        return RSSPullService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    System.out.println("hehehehehe");
    beaconManager = new BeaconManager(getApplicationContext());
    beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
        @Override
        public void onEnteredRegion(Region region, List<Beacon> list) {
            System.out.println("found a beacon");
            if (!list.isEmpty()) {
                Beacon nearestBeacon = list.get(0);


                String beaconKey = String.format("%d:%d", nearestBeacon.getMajor(), nearestBeacon.getMinor());
                if(beaconKey.equals("5310:38023"))
                {
                    showNotification(
                            "Dark Blue.",
                            "Dark Blue");
                }
                else if(beaconKey.equals("57356:56261"))
                {
                    showNotification(
                            "Open Green.",
                            "Open Green");
                }
                else if(beaconKey.equals("23273:12970"))
                {

                    showNotification(
                            "Open Blue.",
                            "Open Blue");
                }
                else
                {

                }
            }

        }
        @Override
        public void onExitedRegion(Region region) {
            // could add an "exit" notification too if you want (-:
        }
    });
    beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
        @Override
        public void onServiceReady() {
            beaconManager.startMonitoring(new Region("monitored region",
                    UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), 23273, 12970));
        }
        // //23273:12970
    });


    beaconManager2 = new BeaconManager(getApplicationContext());
    beaconManager2.setMonitoringListener(new BeaconManager.MonitoringListener() {
        @Override
        public void onEnteredRegion(Region region, List<Beacon> list) {
            System.out.println("Open Green");
            showNotification(
                    "Open Green.",
                    "Open Green");
        }
        @Override
        public void onExitedRegion(Region region) {
            // could add an "exit" notification too if you want (-:
        }
    });
    beaconManager2.connect(new BeaconManager.ServiceReadyCallback() {
        @Override
        public void onServiceReady() {
            beaconManager.startMonitoring(new Region("monitored region",
                    UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), 57356, 56261));
        }
        // //57356:56261
    });


    beaconManager3 = new BeaconManager(getApplicationContext());
    beaconManager3.setMonitoringListener(new BeaconManager.MonitoringListener() {
        @Override
        public void onEnteredRegion(Region region, List<Beacon> list) {
            System.out.println("Dark Blue");
            showNotification(
                    "Dark Blue.",
                    "Dark Blue");
        }
        @Override
        public void onExitedRegion(Region region) {
            // could add an "exit" notification too if you want (-:
        }
    });
    beaconManager3.connect(new BeaconManager.ServiceReadyCallback() {
        @Override
        public void onServiceReady() {
            beaconManager.startMonitoring(new Region("monitored region",
                    UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), 5310, 38023));
        }

    });
    //5310:38023

    System.out.println("khoutan");


    return mBinder;
}
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(android.R.drawable.ic_dialog_info)
            .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);
}


/** method for clients */
public int getRandomNumber() {
    return mGenerator.nextInt(100);
}}

那么解决方案是什么?

0 个答案:

没有答案