-1

As the title says, How can i to send WifiP2pManager and Channel objects to another activity, if possible via Parcelable interface.

I am not that fluent with java and Android SDK so an example code is very much helpful.

Thanks.

  • Show us a code you have already tried. – umka May 31 '15 at 07:41
  • I don't have any working code. I don't know how to. We know that we can send Normal Datatype via intent.putExtra() but this is WifiP2pManager, i don't know how to. – ShootingKIng May 31 '15 at 15:30

3 Answers3

0

in case you do need to share the instance of p2p manager & channel with activities, I would really suggest you to think better ways on achieving the task than simply sending it over.

one way to get it done would be to implement the shared parts in other class that can be used in all activities, for example simply create new class that extends Application. Implement the WifiP2pManager and Channel handling in it, Mark that to be your application in manifest.

Then in activity use getApplicationContext() and cast it to your class and use any public functions to fetch the WifiP2pManager and Channel objects.

Dr.Jukka
  • 2,346
  • 2
  • 15
  • 20
0

After figuring out that we cannot send such objects via intents, i tried to share data between my activities, so static SharedData Class is born in my MainHomeActivity.

public static class SharedData
{
    private static WifiP2pManager mManager;
    private static WifiP2pManager.Channel mChannel;

    static public void set(WifiP2pManager m, WifiP2pManager.Channel c)
    {
        mManager = m;
        mChannel = c;
    }

    static public WifiP2pManager getManager()
    {
        return mManager;
    }

    static public WifiP2pManager.Channel getChannel()
    {
        return mChannel;
    }
}

And used it as follows, Calling activity,

Intent intent = new Intent(view.getContext(), Message.class);
SharedData.set(mManager, mChannel);
startActivity(intent);

and in Called activity,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //...
    Intent intent = getIntent();
    mManager = new ManetsManager(MainHomeActivity.SharedData.getManager(), MainHomeActivity.SharedData.getChannel());
    //...
}

I don't know to what extent this is correct but it worked in my case. Hope this will be helpful for some people. Thanks for your answer @Dr.Jukka

0

Create a static Wifip2pmanager object and initialize manager

public class secondActivity extends AppCompatActivity {
WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;
public static WifiP2pManager sharedManager;
public static WifiP2pManager.Channel sharedChannel;

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

    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
    sharedManager=mManager;
    sharedChannel=mChannel;
 }
}

In another activity call that static object

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    WifiP2pManager manager=secondActivity.sharedManager;
    WifiP2pManager.Channel channel=secondActivity.sharedChannel;
  }
bharath
  • 1
  • 2