我想从活动A获取数据到服务S然后该服务将数据发送到活动D.

时间:2015-04-06 20:24:32

标签: android android-activity service

我正在做一个Android应用程序>如何从活动中获取数据到服务,然后此服务将发出通知并将数据发送到另一个活动,用户可以在活动中查看结果?

我想将用户的id发送到服务并将服务数据放在通知

public class Publication extends Activity {
    final String EXTRA_id = null;
    final String EXTRA_id_user = null;
    final String EXTRA_pseudo = "user_pseudo";
    GPSTracker gps;

    @SuppressWarnings("unused")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publication);

        final Intent publ = getIntent();
        TextView pseudoDisplay = (TextView) findViewById(R.id.pseudview);

        Intent intent1 = new Intent(this, GPSTracker.class);

        startService(intent1);



        gps = new GPSTracker(Publication.this);

        // check if GPS enabled
        if (gps.canGetLocation()) {

            final double latitude = gps.getLatitude();
            final double longitude = gps.getLongitude();

            final String lati = Double.toString(latitude);
            final String longi =Double.toString(longitude);

            Intent intent = new Intent(this, Near_Best.class);
            Bundle b = new Bundle();
            b.putString("Array", publ.getStringExtra(EXTRA_id));
            b.putString("lati", lati);
            b.putString("longi", longi);
            intent.putExtras(b);
            startService(intent);


            // \n is for new line
            Toast.makeText(
                    Publication.this,
                    "Your Location is - \nLat: " + latitude + "\nLong: "
                            + longitude, Toast.LENGTH_LONG).show();
        } else {
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
        }




        if (publ != null) {
            pseudoDisplay.setText(publ.getStringExtra(EXTRA_pseudo));
            final String id = publ.getStringExtra(EXTRA_id);
        }

        final Button search = (Button) findViewById(R.id.search);
        final Button mespub = (Button) findViewById(R.id.mypu);

        search.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent search_i = new Intent(Publication.this, Recherche.class);

                final String id_user = publ.getStringExtra(EXTRA_id);
                search_i.putExtra(EXTRA_id, id_user);
                startActivity(search_i);
            }

        });
        mespub.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent Myp = new Intent(Publication.this, Recherchepub.class);

                final String id_use = publ.getStringExtra(EXTRA_id);
                Myp.putExtra(EXTRA_id_user, id_use);
                startActivity(Myp);

            }

        });

        findViewById(R.id.pubmulti).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent test = new Intent(Publication.this,
                        PhotoPublication.class);
                final String id_use = publ.getStringExtra(EXTRA_id);
                test.putExtra(EXTRA_id, id_use);
                final String pseu = publ.getStringExtra(EXTRA_pseudo);
                test.putExtra(EXTRA_pseudo, pseu);
                startActivity(test);
            }

AND THE SERVICE S ,I want to use the id in this service

    public class Near_Best extends Service implements Runnable, LocationListener {
        private LocationManager lManager;
        private Handler handler;

        @Override
        public void onCreate() {
            handler = new Handler();
            lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    this).setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Near best")
                    .setContentText("notification service !");
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(this, Affichage.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(Affichage.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(0, mBuilder.build());
            Thread thread = new Thread(this);
            thread.start();
        }

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    // On récupère le service de localisation
                    handler.post(new Runnable() {
                        @Override
                        public void run() {

                            lManager.requestLocationUpdates("network", 60000, 0,
                                    Near_Best.this);
                        }
                    });
                    Thread.sleep(10 * 60 * 1000);
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    @Override
    public void onLocationChanged(Location location) {

        final String latitude = String.valueOf(location.getLatitude());
        final String longitude = String.valueOf(location.getLongitude());



        new Thread(new Runnable() {
            @Override
            public void run() {
                String result;
                try {
                    result = UserFunctions.service("", latitude,
                            longitude);
                } catch (Throwable e) {

                    e.printStackTrace();
                }
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(Near_Best.this, "non",
                                Toast.LENGTH_LONG).show();

                    }

                });
            }
        }).start();
        ;
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:0)

ID作为额外内容添加到您在Intent中使用的startService()。在Service onStartCommand()中,您可以使用IDIntent检索getXXXExtra()