How can I pass ArrayListFile to a service

1

I want to pass an ArrayList File to a service to visualize its data but it is not possible for me in the line that receives the information, the program stops, this is the code

Method to send it

public void SendToService(){
    Intent service=new Intent(MainActivity.this,ForegroundService.class);
    if(ForegroundService.IS_SERVICE_RUNNING){

        AdapterService=recibidor_file; 

        service.setAction(Constantes.ACTION.DATA_ACTION);
        ForegroundService.IS_SERVICE_RUNNING=true;
        service.putExtra("key",AdapterService);

        startService(service);

    }
}

This is the service

 public int onStartCommand(Intent intent,int flags,int StarId){

    recibidor=(ArrayList)intent.getExtras().getSerializable("key");
    titulo=String.valueOf(recibidor);

} Thank you in advance, this problem has a big head where I'm looking for getIntent talk but it does not work within a Servcio

Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.a7860k.reproductor, PID: 8787
              java.lang.RuntimeException: Unable to start service com.example.a7860k.reproductor.ForegroundService@384fbbd1 with Intent { act=com.example.a7860k.reproductor.action.startforeground cmp=com.example.a7860k.reproductor/.ForegroundService (has extras) }: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList
                  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2911)
                  at android.app.ActivityThread.access$2100(ActivityThread.java:151)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
               Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList
                  at com.example.a7860k.reproductor.ForegroundService.onStartCommand(ForegroundService.java:82)
                  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
                  at android.app.ActivityThread.access$2100(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Application terminated.

    
asked by Felipe Peña 16.11.2017 в 04:25
source

2 answers

1

I managed to solve it, thanks Einer I had to change a small thing, first the data is sent from the main activity in a normal way

SentToServer

 public void SendToService(){
    Intent service=new Intent(MainActivity.this,ForegroundService.class);
    if(ForegroundService.IS_SERVICE_RUNNING){

        ArrayList<File>AdapterService=new ArrayList<>();

        AdapterService=recibidor_file;

        service.putExtra("Files",AdapterService);
        startService(service);

    }
}

Then just receive the data with:

ArrayList<File>recibidor_file=new ArrayList<>();    
recibidor_file= (ArrayList<File>) intent.getExtras().get("Files");
    
answered by 21.11.2017 / 00:15
source
-1

Try instead to send a ArrayList of File , send the url of the files as a string using the method Intent#putStringArrayListExtra to send it and to receive the method Intent#getStringArrayListExtra .

To send it, it would be like this:

public void SendToService(){


    Intent service=new Intent(MainActivity.this,ForegroundService.class);
    if(ForegroundService.IS_SERVICE_RUNNING){

        AdapterService=recibidor_file; 


        ArrayList<String> urls = new ArrayList<>();
        for(File f : recibidor_file)
        {
          urls.add(f.getAbsolutePath());
        }

        service.setAction(Constantes.ACTION.DATA_ACTION);
        ForegroundService.IS_SERVICE_RUNNING=true;
        service.putStringArrayListExtra("key", urls);

        startService(service);

    }
}

And to receive them in the service would be like this:

public int onStartCommand(Intent intent,int flags,int StarId){

    ArrayList<String> filesUrls =(ArrayList)intent.getExtras().getStringArrayListExtra("key");

    ArrayList<File> files = new ArrayList<>();
    for(String fPath : filesUrls)
    {
        files.add(new File(fPath));
    }

    recibidor = files;
    }
    
answered by 16.11.2017 в 04:56