service send information to Activity

by maggy mtac » Tue, 15 Mar 2011 17:21:21 GMT


Sponsored Links
 Hello,

I want to known How send information for example String from service
to Activity?

How start even Activity in a service because i want to display when
the service recover a data , he transmit to even Activity?

Thanks

-- 



Re: service send information to Activity

by Mark Murphy » Tue, 15 Mar 2011 20:01:47 GMT


 


Send a broadcast Intent.

Or, have the activity supply a Messenger to the service via an Intent
extra, and the service sends a message to the activity via that
Messenger.

Or, have the activity use createPendingResult() to create a
PendingIntent and pass that via an Intent extra, and the service
executes that PendingIntent, providing a response that the activity
receives in onActivityResult().

Or, if you are using bindService(), have the activity register some
sort of listener with the service, which the service uses to send back
data.

There are probably other options as well.


Do not "start even Activity in a service", unless you KNOW that the
user is using your application in the foreground. Use a Notification
to alert the user that "the service recover a data", please.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

Warescription: Three Android Books, Plus Updates, One Low Price!

-- 


Sponsored Links


Other Threads

1. Help please...

Hi group, I still have not been able to get some assistance in
retrieving contact data and I could really use the help. Right now, the
only thing I can access without any null pointer or cursor out of bounds
exceptions being thrown is the contacts display name. Does anyone know
how to access a phone number as well as the thumbnail icon associated
with a contact? Thanks.

package com.grey.gui;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainPage extends ListActivity {

        private Cursor c;
        private ContentResolver cr;
        
        @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        cr = getContentResolver();
        c = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,
null,null);

        this.setListAdapter(new MyContactsAdapter(this,c));
        
     
}
        @Override
        public void onListItemClick(ListView l, View v, int position, long id){
                 super.onListItemClick(l, v, position, id);      
                 Intent newIntent = new Intent(Intent.ACTION_MAIN); 
                 newIntent.setClass(this, Summary.class);       
                 startActivity(newIntent);
            }

          private class MyContactsAdapter extends CursorAdapter{

                    private Cursor mCursor;
                    private Context mContext;
                    private final LayoutInflater mInflater;

                    public MyContactsAdapter(Context context, Cursor cursor) {
                      super(context, cursor, true);
                      mInflater = LayoutInflater.from(context);
                      mContext = context;
                    }


                    private TextView t;
                    @Override
                    public void bindView(View view, Context context, Cursor 
cursor) {
                        String name =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Log.d("Name",name);
                        t = (TextView) view.findViewById(R.id.txtName);
                        
                        if(name != null) t.setText(name); 
                        
                        String hasNumber =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                        Log.d("hasNumber", hasNumber);
                      //        if(hasNumber.equals("1")){
                        String number =
cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.NUMBER));
                                //Log.d("Number", number);
                                //TextView a = (TextView) 
view.findViewById(R.id.phoneNumber);
                                //a.setText(number);
                      //        }
                          /*ImageView i = (ImageView) 
view.findViewById(R.id.icon);
                          byte[] img =
cursor.getBlob(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
                          Bitmap b = BitmapFactory.decodeByteArray(img, 0, 
img.length);
                          i.setImageBitmap(b);*/
                    }



                    @Override

                    public View newView(Context context, Cursor cursor, 
ViewGroup
parent) {
                      final View view = mInflater.inflate(R.layout.main, parent,
false);
                      return view;

                    }

                  }


}




-- 

2. Is SignalStrength readable outside onSignalStrengthschanged?

With SDK 2.1 it is possible to get some additional signalstrength
information in the Phonestatelisteners onSignalStrengthsChanged event.
That works, but I need to monitor the signalstrength more accurate.
onSignalStrengthsCanged is only fired if one of the four signal bars
of the signalstrength indicator have changed.

So I tried this outside:

SignalStrength sig new SignalStrength();
rxl = sig.getGsmSignalStrength();

But it tells me that SignalStrength() is not visible. Why is that? The
funny thing is, that the NeighbouringCellInfo gives me the
Signalstrength of the neighbourcells every second, if I want. But the
serving cell is updated very badly.

Also I can use getGsmBitErrorRate() only in onSignalStrengthsChanged.
That makes no sense, because the quality of the signal is largely
independent of the strength.

So is there a change of getting the actual signalstrenth outside
onSignalStrengthsChanged?

-- 

3. Ada anak jogja gak neh ?

4. Android-2.1 eclair branch on omap3430 gives blank screen

5. Thread always seem to throw exceptions although no erros in program

6. Mo nanya tentang cache

7. Thread always seem to throw exceptions although no erros in program