2011-06-15 8 views
8

बनाई गई मुख्य गतिविधि की तुलना में किसी पृष्ठभूमि से डेटा/थ्रेड को किसी अन्य गतिविधि से कैसे पास किया जाए, मैंने एक सरल एंड्रॉइड एप्लिकेशन बनाया है ताकि एक हैंडलर या हैंडलर का उपयोग डेटा से पास करने के लिए किया जा सके। बैकग्राउंड सर्विस/थ्रेड कुछ अन्य गतिविधि मुख्य गतिविधि के अलावा पृष्ठभूमि सेवा बनाई गई है। मुझे मेनएक्टिविटी में काम करने वाली सेवा, धागा और एक हैंडलर मिला है। अंतिम चरण हैडलर को डेटा एक्टिविटी के अलावा कुछ अन्य गतिविधि पर डेटा पास करने के लिए है। मैं मेनएक्टिविटी के हैंडलर को संदेश पास करने के लिए सेवा प्राप्त कर सकता हूं, लेकिन मुझे नहीं पता कि इसे पर कुछ अन्य गतिविधि पर डेटा पास करने के लिए कैसे प्राप्त किया जाए।संशोधन 2: बैकग्राउंड सेवा

कोई ऐसा क्यों करना चाहेगा? मैंने सोचा कि यह एक साधारण एमपी 3 प्लेयर की तुलना में है, लेकिन वास्तव में यह तुलना करता है कि यह एक अच्छा एफएम रेडियो है। मुख्य गतिविधि एक पृष्ठभूमि सेवा का उपयोग करती है जो मुझे एफएम स्टेशन का चयन करने की अनुमति देती है। जब मैं प्ले गतिविधि लॉन्च करता हूं तो उसे उसी पृष्ठभूमि सेवा से जोड़ना चाहिए ताकि मैं सुनना जारी रख सकूं जबकि (अच्छा भाग) ऑडियो के ग्राफ़िक तुल्यकारक या एनीमेशन को प्रदर्शित करता है। असल में, मुझे नहीं पता कि एक से अधिक गतिविधियों से पृष्ठभूमि सेवा से कैसे जुड़ना है।

मेरा कोड मूल रूप से Pro Android 2 में सेवा उदाहरण पृष्ठ 304 पर आधारित था और CommonsWare Sample Application द्वारा बहुत मदद मिली थी।

कृपया मेरे वर्तमान कोड पर एक नज़र डालें। यह तीन ध्यान से टिप्पणी की गई फ़ाइलों का वर्णन मैं क्या करने की कोशिश कर रहा हूँ और कठिनाइयों मैं डेटा को MainActivity के अलावा कुछ अन्य गतिविधि गुजर कर रहा हूँ के होते हैं: एक पृष्ठभूमि सेवा का उपयोग

/************************************************************************************************** 
* File: MainActivity.java 
* Application: BackgroundService 
* Description: This file contains the main activity that is run when the BackgroundService 
*  application is launched. 
**************************************************************************************************/ 

package com.marie.mainactivity; 

import com.marie.mainactivity.BackgroundService; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.os.Messenger; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

/* 
* Class: MainActivity 
* Purpose: Using a button, the MainActivity class starts the backgroundService and 
*  the RcvMessages activity. Using another button MainActivity stops the backgroundService. 
*  NOTE: RcvMessages is only a stub that does nothing but display a simple message. 
* Handler: MainActivity defines and provides a reference to "handler" for the backgroundService. 
*/ 
public class MainActivity extends Activity { 
    private static final String TAG = "MainActivity"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Log.d(TAG, "starting service"); 

     /* 
     * The bind button: bindBtn 
     * Clicking this button starts the background Service and launches the 
     * RcvMessages activity. NOTE: RcvMessages is only a stub so far. 
     */ 
     Button bindBtn = (Button)findViewById(R.id.bindBtn); 
     bindBtn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       // Start the background Service for sending canned messages to the handler as a test. 
       Intent backgroundService = new Intent(MainActivity.this, com.marie.mainactivity.BackgroundService.class); 
       backgroundService.putExtra(BackgroundService.EXTRA_MESSENGER, new Messenger(handler)); 
       startService(backgroundService); 

       // Start the RcvMessages activity to receive messages from the handler. But how??? 
       Intent messages = new Intent(MainActivity.this, com.marie.mainactivity.RcvMessages.class); 
       startActivity(messages); 
      } 
     }); 

     /* 
     * The unbind button: unbindBtn 
     * Clicking this button stops the background Service. 
     */ 
     Button unbindBtn = (Button)findViewById(R.id.unbindBtn); 
     unbindBtn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       // Stop the background Service 
       Intent backgroundService = new Intent(MainActivity.this, BackgroundService.class); 
       stopService(backgroundService); 
      } 
     }); 
    } 

    /* 
    * This is the handler to be passed to the background Service via a Messenger. 
    * NOTE: I want it to send messages to my RcvMessages activity. 
    */ 
    private Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      // simple handler test (does not send messages to RcvMessages activity 
      String obj = (String) msg.obj; 
      Log.i("handleMessge", "obj: " + obj); 
     } 
    }; 
} 

/************************************************************************************************** 
* File: BackgroundService.java 
* Application: BackgroundService 
* Description: This file contains the background Service that is launched by the MainActivity's 
*  bind button. 
**************************************************************************************************/ 

package com.marie.mainactivity; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.os.Message; 
import android.os.Messenger; 
import android.os.RemoteException; 

/* 
* Class: BackgroundService 
* Purpose: Using the onStart() method the BackgroundService gets the reference to the 
*  Messenger instance that was passed to BackgroundService. The messenger is then 
*  used by the ServiceWorker() thread to send messages to the handler that is defined 
*  in the MainActivity class. 
*/ 
public class BackgroundService extends Service { 
    private NotificationManager notificationMgr; 

    public static final String EXTRA_MESSENGER = "com.marie.mainactivity.EXTRA_MESSENGER"; 
    private Messenger messenger; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     notificationMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

     displayNotificationMessage("starting Background Service"); 

     Thread thr = new Thread(null, new ServiceWorker(), "BackgroundService"); 
     thr.start(); 
    } 

    /* 
    * This is the ServiceWorker thread that passes messages to the handler defined in 
    * the MainActivity class. 
    * NOTE: Instead of passing messages to a handler in MainActivity I would like 
    * it to pass messages to a handler defined in the RcvMessages activity. 
    */ 
    class ServiceWorker implements Runnable 
    { 
     public void run() { 
      // do background processing here... something simple 

      // send a message to the handler defined in the MainActivity class 
      try { 
       Message msg1 = Message.obtain(); 
       msg1.obj = "Hello 1"; 
       messenger.send(msg1); 
      } catch (RemoteException e) { 
       e.printStackTrace(); 
      } 

      // stop the service when done... 
      // BackgroundService.this.stopSelf(); 
      // Or use the unbindBtn in the MainActivity class. 
     } 
    } 

    @Override 
    public void onDestroy() 
    { 
     displayNotificationMessage("stopping Background Service"); 
     super.onDestroy(); 
    } 

    /* 
    * onStart is where we get our reference the Messenger that allows 
    * us to send messages to the handler defined in the MainActivity class. 
    */ 
    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 

     Bundle extras = intent.getExtras(); 
     messenger = (Messenger)extras.get(EXTRA_MESSENGER); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    private void displayNotificationMessage(String message) 
    { 
     Notification notification = new Notification(R.drawable.note, message, System.currentTimeMillis()); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); 

     notification.setLatestEventInfo(this, "Background Service", message, contentIntent); 

     notificationMgr.notify(R.id.app_notification_id, notification); 
    } 
} 

/************************************************************************************************** 
* File: RcvMessages.java 
* Application: BackgroundService 
* Description: This file contains stub code that displays a test message in an EditText. 
**************************************************************************************************/ 

package com.marie.mainactivity; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.InputType; 
import android.widget.EditText; 

/* 
* Class: RcvMessages 
* Purpose: RcvMessages is stub code that I want to extend in some way to receive messages from 
*  the background Service. 
*  NOTE: I don't know who to do this. 
*/ 
public class RcvMessages extends Activity { 

    EditText myText; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.messages); 

     myText = (EditText)findViewById(R.id.my_text); 

     myText.setSingleLine(); 
     myText.setInputType(InputType.TYPE_NULL); 

     // Display a simple test message for now. 
     myText.setText("RcvMessages here"); 
    } 
} 

किसी भी मदद की, धागा , और हैंडलर को पर डेटा पास करने के लिए कुछ अन्य गतिविधि को मुख्य गतिविधि के अलावा बैकग्राउंड सेवा की सराहना की जाएगी।

+0

में MyApplication नाम सेट करने मैं एक जवाब प्राप्त करने की उम्मीद में काफी इस सवाल का संशोधित किया है। – Marie

उत्तर

8

Handler विशिष्ट धागे से जुड़ा हुआ है, इसलिए जब तक आप इसे यूआई थ्रेड में बनाते हैं, तब तक आपको हैंडलर साझा करना होगा। मैं अपने हैंडलर को अपने Application कक्षा में रखूंगा। और यदि आप चाहें तो पूरे Messenger डाल सकते हैं। फिर आप ((MyApplication)getApplication()).getHandler() के माध्यम से किसी भी गतिविधि में हैंडलर तक पहुंच सकते हैं। जब गतिविधि शुरू होती है या रोकती है, तो आप उसे कॉलबैक के रूप में पंजीकृत कर सकते हैं।

तो यह

public class MyApplication extends Application { 
    Handler h = new Handler() { 
     public void handleMessage(Message m) { 
     if (realCallback!=null) { 
      realCallback.handleMessage(m); 
     } 
     } 
    }; 
    Handler.Callback realCallback=null; 
    ...... 
    public Handler getHandler() { 
     return h; 
    } 

    public void setCallback(Handler.Callback c) { 
     realCallback = c; 
    } 
} 

किसी भी गतिविधि दूत

public class MyActivity extends Activity implements Handler.Callback 

...... 

public void onStart() { 
    ((MyApplication)getApplication()).setCallback(this); 
} 

public void onPause() { 
    ((MyApplication)getApplication()).setCallback(null); 
} 

public void handleMessage(Message msg) { 
    //.... Do stuff .... 
} 

}

के माध्यम से अनुरोध प्राप्त होता है क्या करने की जरूरत है कि में की तरह कुछ यह सिर्फ एक विचार है। आपको इसे अपनी जरूरतों के अनुरूप ट्यून करने की आवश्यकता हो सकती है।

और भूल नहीं है AndroidManifest.xml

+0

मुझसे पूछने के लिए क्षमा करें, लेकिन अगर आपको मेरा प्रश्न पसंद आया तो आप इसे वोट देने के बारे में सोच सकते हैं। – Marie

+0

एलेक्स- "एंड्रॉइडमैनीफेस्ट.एक्सएमएल में MyAplication नाम सेट करना न भूलें" Thx –

+1

@Jib, आपको मेनिफेस्ट में एप्लिकेशन का सही वर्णन करने की आवश्यकता है। विशेष रूप से अपना नाम सेट करें। यह थोड़ी देर हो गया है इसलिए मुझे इस मामले के विनिर्देश याद नहीं हैं। –

संबंधित मुद्दे