سؤال

I have created an activity called viewActivity. It displays the shared files and folders in windows system. I am using the function listFiles() from jcifs.smb package. Initially the shared drives are displayed. I have made an option to call the listFiles() function recursively. That is when I click a particular folder in a drive, the contents of the file are displayed. When I once again click the folder which is inside the previous folder, its content are also displayed. The problem here is when I press the back button in android, it calls the previous activity instead of going back to previous folder. So how to achieve this functionality?

 //my package
package com.android.accesspc;

import java.net.MalformedURLException;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class viewActivity extends Activity{

int id=0;
SmbFile previous=null;
int count=0;
void list(SmbFile obj)
{
    ScrollView sv = new ScrollView(this);
    LinearLayout layout=new LinearLayout(this);
    layout.setOrientation(0x00000001);
    try
    {
        SmbFile a[]=obj.listFiles();
        for(final SmbFile m:a)
        {
            if(!m.isHidden())
            {
                Button btn = new Button(this); 
                btn.setId(id); 
                if(m.isDirectory())
                {
                    btn.setText(m.getName().replace("/","")); 
                    btn.setTextSize((float)20);
                    btn.setOnClickListener(new 
                    View.OnClickListener()
                    {
                        public void onClick(View v) 
                        {
                previous=m;//used to hold previous smb object
                count++;//takes count of no of folders pressed 
                            list(m);
                        }
                    });
                }
                else
                {
                    btn.setText(m.getName()); 
                }
                layout.addView(btn);
                id++;
            }
        }
        sv.addView(layout);
        setContentView(sv);
    }
    catch(SmbException e)
    {
Toast displayMsg=Toast.makeText(this,"cannot list files",Toast.LENGTH_SHORT);
        displayMsg.show();
    }
}

String ip,name,password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);

    ip="192.168.0.100";//my ip address
    name="abc";//my windows username
    password="abcd";//my windows password

    SmbFile dir=null;
    String url= "smb://" + ip + "/";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, name, password);
    try 
    {
        dir = new SmbFile(url, auth);
    } 
    catch (MalformedURLException e1) 
    {
    Toast displayMsg=Toast.makeText(this,"Network error",Toast.LENGTH_SHORT);
        displayMsg.show();
    }
    //used to list files and folders of smb object initially
    list(dir);
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    if(!(count==0))
    {
        //if count is not 0,that means the user has clicked a folder
    //we have "previous" holding previous smb value which  has to be called now
        list(previous);
    }
    else
    {
        //the default behaviour
        super.onBackPressed();
    }
}

}
هل كانت مفيدة؟

المحلول

Override the super.onBackPressed()

 @Override
 public boolean onBackPressed(){

 //your code
 }

Should work on 2.3.3

نصائح أخرى

You need to override the behavior of back button:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //your code here
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
//my package
package com.android.accesspc;

import java.net.MalformedURLException;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class viewActivity extends Activity{

int id=0;
SmbFile previous=null;
int count=0;
void list(SmbFile obj)
{
    ScrollView sv = (ScrollView) findViewById(R.id.scrollview);
    LinearLayout layout= (LinearLayout) findViewById(R.id.linearlayout);
    //layout.setOrientation(0x00000001); set this in your xml file
    try
    {
        SmbFile a[]=obj.listFiles();
        for(final SmbFile m:a)
        {
            if(!m.isHidden())
            {
                Button btn = new Button(this); 
                btn.setId(id); 
                if(m.isDirectory())
                {
                    btn.setText(m.getName().replace("/","")); 
                    btn.setTextSize((float)20);
                    btn.setOnClickListener(new 
                    View.OnClickListener()
                    {
                        public void onClick(View v) 
                        {
                previous=m;//used to hold previous smb object
                count++;//takes count of no of folders pressed 
                            list(m);
                        }
                    });
                }
                else
                {
                    btn.setText(m.getName()); 
                }
                layout.addView(btn);
                id++;
            }
        }
    }
    catch(SmbException e)
    {
Toast displayMsg=Toast.makeText(this,"cannot list files",Toast.LENGTH_SHORT);
        displayMsg.show();
    }
}

String ip,name,password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);

    ip="192.168.0.100";//my ip address
    name="abc";//my windows username
    password="abcd";//my windows password

    SmbFile dir=null;
    String url= "smb://" + ip + "/";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, name, password);
    try 
    {
        dir = new SmbFile(url, auth);
    } 
    catch (MalformedURLException e1) 
    {
    Toast displayMsg=Toast.makeText(this,"Network error",Toast.LENGTH_SHORT);
        displayMsg.show();
    }
    //used to list files and folders of smb object initially
    list(dir);
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    if(!(count==0))
    {
        //if count is not 0,that means the user has clicked a folder
    //we have "previous" holding previous smb value which  has to be called now
        list(previous);
    }
    else
    {
        //the default behaviour
        super.onBackPressed();
    }
}

}

and this is what your layout xml file (named view.xml) looks like this (/res/layout/view.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollview">
<LinearLayout 
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> </LinearLayout>


</ScrollView>

</LinearLayout>

simply call finish() on click.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top