0

嗨,这是我试图获取文本文件的方法。我尝试了同样的方法

  • 新线程

同样,当我创建新线程时,它不会仅出现在 getfiles(Drive services) 方法中。请给我解决方案我没有得到这个。

使用 main 运行错误可能导致死锁

公共类 MainActivity 扩展 Activity {

  static final int REQUEST_ACCOUNT_PICKER = 1;


  private static Uri fileUri;
  private static Drive service;
  private GoogleAccountCredential credential;
  Button b;

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

    b = (Button)findViewById(R.id.b1);

    credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
    startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);

    Toast.makeText(getApplicationContext(), "case1", Toast.LENGTH_LONG).show();

}

 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) 
 {

     if(requestCode==REQUEST_ACCOUNT_PICKER)
     {
         if (resultCode == RESULT_OK && data != null && data.getExtras() != null)
         {
             Toast.makeText(getApplicationContext(), "case2", Toast.LENGTH_LONG).show();

                String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                if (accountName != null)
                {
                  credential.setSelectedAccountName(accountName);
                  service = getDriveService(credential);

                  Toast.makeText(getApplicationContext(), "Got the E mail id", Toast.LENGTH_LONG).show();
             b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "case4", Toast.LENGTH_LONG).show();

                    getfiles(service);

                }
              });
          }
        }
     }


 }

 public void getfiles(final Drive service)
 {



     Toast.makeText(getApplicationContext(), "case5", Toast.LENGTH_LONG).show();
     String TEXT_PLAIN = "TEXT/PLAIN";
     try
     {
     Files.List request = service.files().list().setQ("mimeType = '" + TEXT_PLAIN +"'");
     Toast.makeText(getApplicationContext(), "case", Toast.LENGTH_LONG).show();                 
     Map<String, File> textFiles = new HashMap<String, File>();

            do {

                  Toast.makeText(getApplicationContext(), "case6", Toast.LENGTH_LONG).show();

                FileList files = request.execute();  //this is error line


                Toast.makeText(getApplicationContext(), "case7", Toast.LENGTH_LONG).show();
                for (File file : files.getItems())
                {
                  textFiles.put(file.getId(), file);
                }
                request.setPageToken(files.getNextPageToken());

            } while (request.getPageToken() != null && request.getPageToken().length() > 0);

     }
     catch(Exception e)
     {
         Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
     }



 }


 private Drive getDriveService(GoogleAccountCredential credential) {
        return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
            .build();
      }

@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;
}

}

4

1 回答 1

0

您应该执行任何可能需要很长时间的代码,例如不同线程上的 I/O 或网络活动。在您的情况下,您的文件检索最好在 ASyncTask 中完成。

这个警告的原因是因为当你的代码被执行以从 Drive 中检索文件时,代码将阻塞直到文件检索完成,这意味着程序中的任何其他内容都无法响应,即 GUI 不会更新并且来自尝试使用 GUI 做事的用户的输入将不会被注册。几秒钟后,如果文件检索仍未完成,则会向用户显示 ANR(应用程序未响应)消息。这将让用户选择等待,如果他们的病人,或者很可能强制关闭您的应用程序。

于 2013-08-23T09:25:50.337 回答