user3780902
user3780902

Reputation: 15

How to write adb commands inside a Android application?

I want to know how to execute adb commands from within my code. For e.g I want to push a file inside the adb shell and I write this:

package org.example.adbshell;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

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

        Process process;
        try {
            process = Runtime.getRuntime().exec("adb push C:/Users/Savio/Desktop/savio.xml /storage/sdcard0/");
            BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

I should be able to see the savio.xml inside the /storage/sdcard0 folder. But for some reason I am unable to see the file. I am guessing that the command is not getting executed. What am I doing wrong here?

Upvotes: 0

Views: 7611

Answers (2)

Lavekush
Lavekush

Reputation: 6166

There are three type of command

  1. System command // In this case below code is working. like mv/edit/cp/cd etc
  2. Non routed command
  3. Rooted command // You need to routed device.

Try this

Process process Runtime.getRuntime().exec("your command");

//or

Process process Runtime.getRuntime().exec("/path/your_command");

Its work for me of copying file :

Process process Runtime.getRuntime().exec("/system/bin/mv my_file_path");

You can read output data with the help of process object

// Use buffer reader for the same.
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));

Upvotes: 1

Václav Hodek
Václav Hodek

Reputation: 668

There is no ADB command on Android's side. ADB is a part of Android SDK only and is always run on computer not on Android.

Also, Android knows nothing about C:/Users/Savio/Desktop/savio.xml as it is not a file in its filesystem.

You should rather implement desktop application with HTTP (or similar) server and use it to download and upload files from Android to PC. You can start with NanoHTTP, a lightweight HTTP implementation.

Or you can use server for communication between Android and computer and run ADB command on that computer based upon request from Android.

Upvotes: 0

Related Questions