2015-04-06 98 views
0

我正在做一個android應用程序>我如何從活動獲取數據到服務,然後這個服務會發出通知併發送數據到另一個活動,用戶可以在活動中看到結果?我想從活動A到服務S的數據,然後這個服務將數據發送到活動D

我要發送的用戶服務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

提出具體問題。 – Ryan

+0

我想從活動發送數據到服務,請在服務中使用它? –

+0

從你的問題來看,你並不十分清楚你需要什麼。在服務中爲您當前使用的ID添加一個成員變量就足夠了嗎? –

回答

0

作爲額外添加ID您在startService()使用Intent。在您的Service,onStartCommand()中,您可以使用getXXXExtra()Intent中檢索ID