2012-04-03 68 views
0

我创建了一个名为viewActivity的活动。它显示Windows系统中的共享文件和文件夹。我使用jcifs.smb包中的函数listFiles()。最初显示共享驱动器。我已经做出了递归调用listFiles()函数的选项。这是当我点击驱动器中的特定文件夹时,文件的内容显示。当我再次单击位于上一个文件夹内的文件夹时,其内容也会显示出来。这里的问题是当我在android中按下后退按钮时,它会调用前一个活动,而不是返回到上一个文件夹。那么如何实现这个功能呢?如何回到以前的活动状态?

//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(); 
    } 
} 

} 
+0

对不起。感谢您的指导。 – raghs 2012-04-03 15:43:12

回答

1

重写super.onBackPressed()

@Override 
public boolean onBackPressed(){ 

//your code 
} 

应在2.3.3

+0

hai谢谢你的回复。我已经实现了它。但它不工作。请看看我的代码,并请说出您的意见。 – raghs 2012-04-03 15:23:11

1

你需要重写后退按钮的行为:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     //your code here 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
+0

但是这段代码在android 2.3.3 – raghs 2012-04-03 14:57:58

0

只需拨打finish()上点击。

0
//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(); 
    } 
} 

} 

工作,这是你的布局XML文件(名为view.xml用)看起来像这样(/ 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>