2016-11-22 47 views
0

我有一個viewpager片段的Android活動。在活動的onCreate方法中,我將一個服務綁定到在後臺不斷運行的服務。如何訪問綁定到Android中父級活動的片段中的服務功能?

在片段中,在一定的條件下,我需要調用處理條件的服務中的函數。訪問該服務的正確方法是什麼,並將其稱爲功能?

MainActivity.java

public class MainActivity extends AppCompatActivity { 

private String TAG = "MainActivity"; 

DbHelper dbHelper; 
SessionManager sessionManager; 
SessionCache sessionCache; 
Map<String, ?> userDetails; 
protected SocketListener socketService; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    sessionManager = new SessionManager(getApplicationContext()); 

    // Connect to background socketlistener service 
    Intent serviceIntent = new Intent(MainActivity.this, SocketListener.class); 
    startService(serviceIntent); 
    . 
    . 
    . 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
    tabLayout.addTab(tabLayout.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.profileicon))); 
    tabLayout.addTab(tabLayout.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.homeicon))); 
    tabLayout.addTab(tabLayout.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.chaticon))); 

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 
    final NonSwipeableViewPager viewPager = (NonSwipeableViewPager) findViewById(R.id.view_pager); 

    final CustomPagerAdapter pagerAdapter = new CustomPagerAdapter 
      (getSupportFragmentManager(), tabLayout.getTabCount()); 

    viewPager.setAdapter(pagerAdapter); 
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 

    viewPager.setCurrentItem(1); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 
} 

    private ServiceConnection serviceConnection = new ServiceConnection() { 


@Override 
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 
     socketService = ((SocketListener.LocalBinder) iBinder).getService(); 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName componentName) { 
     socketService = null; 
    } 
}; 

} 

這是我的服務。我連接到服務器,並不斷收聽更新。

SocketListener.java

package com.example.gopa2000.mobapps; 

import android.app.Service; 
import android.content.Intent; 
import android.os.Binder; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

import java.net.URISyntaxException; 

import io.socket.client.IO; 
import io.socket.client.Socket; 
import io.socket.emitter.Emitter; 

public class SocketListener extends Service { 

private static String TAG = "SocketListener"; 
private Socket socket; 

public SocketListener() { } 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO: Return the communication channel to the service. 
    return localBinder; 
} 

private final IBinder localBinder = new LocalBinder(); 
public class LocalBinder extends Binder { 
    public SocketListener getService(){ 
     Log.i(TAG, "getService: Sitting in local binder."); 
     return SocketListener.this; 
    } 

    public void sendMessage(String message){ 
     socket.emit("match", message); 
    } 
} 

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

public void isBoundable(){ 
    Log.i(TAG, "Bind like a baller."); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent, flags, startId); 

    Runnable connect = new ConnectSocket(); 
    new Thread(connect).start(); 
    return START_STICKY; 
} 

class ConnectSocket implements Runnable { 
    @Override 
    public void run() { 
     try { 
      socket = IO.socket(RESTClient.getURL()); 

      socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { 
       @Override 
       public void call(Object... args) { 
        Log.i(TAG, "call: Connected to backend!"); 
       } 
      }); 

      socket.connect(); 
     } catch (URISyntaxException e){ 
      Log.e(TAG, "run: ", e); 
     } 
    } 
} 

public Socket getSocket(){ 
    return this.socket; 
} 
} 

而這正是我需要調用sendMessage(string)的片段。

public class MainViewFragment extends Fragment { 

private final String TAG = "MVFragment"; 

private RecyclerView recyclerView; 
private RecyclerView.LayoutManager layoutManager; 
private RecyclerView.Adapter adapter; 
private SessionManager sessionManager; 
private ArrayList<CustomCard> cards; 
private CardAdapter cardAdapter; 
private Button btn; 
private SwipeFlingAdapterView flingContainer; 
private Map<String, ?> userDetails; 
// hax 
private static String userEmail; 



@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.from(getContext()).inflate(R.layout.fragment_main_view, container, false); 

      . 
      . 

    flingContainer.setAdapter(adapter); 

    flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener(){ 
      . 
      . 

     @Override 
     public void onRightCardExit(Object o) { 
      . 
      . 

      if(match){ 

      // need to call sendMessage from the service here. <------------------------ 

       sessionCache.addToMatchTable(Liker, Likee); 
      } 
     } 

     @Override 
     public void onAdapterAboutToEmpty(int i) { 

     } 

     @Override 
     public void onScroll(float v) { 

     } 
    }); 


    flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener(){ 
     @Override 
     public void onItemClicked(int itemPosition, Object dataObject){ 
      Toast.makeText(getActivity(), "Clicked!", Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(getActivity().getApplicationContext(), FullScreenCardLayout.class); 
      startActivity(intent); 
     } 
    }); 

    return view; 
} 

回答

1

首先方法sendMessage(message)應該是裏面的服務不是LocalBinder

public class SocketListener extends Service { 

    public void sendMessage(String message){ 
     socket.emit("match", message); 
    } 
} 

其次,從片段訪問活動的方法,你應該創建片段內部接口

interface MessageSender { 
    void sendMessage(String message); 
} 

然後實現它在你的活動

public class MainActivity extends AppCompatActivity implements MessageSender { 

    @Override 
    public void sendMessage(String message) { 
     // call the service here 
     socketService.sendMessage(message); 
    } 
} 

你的片段中,實現onAttach()並初始化接口實例

public class MainViewFragment extends Fragment { 
    private MessageSender mMessageSenderCallback; 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     try { 
      mMessageSenderCallback = (MessageSender) context; 
     } catch (ClassCastException e) { 
      // Error, class doesn't implement the interface 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     // Remove activity reference 
     mMessageSenderCallback = null; 
    } 
} 

要發送信息,撥打mMessageSenderCallback.sendMessage(message)與片內的信息。

相關問題