2017-02-20 9 views
0

私は、Bluetoothを使用するためにFileDialogとIntentを表示する機能を持っています。 しかし、私が戻るボタンを押すと、それは以前のアクティビティになります、それは表示されますが、クリック可能ではありません(スクリーンショットのように)、私はもう一度戻るボタンを押す必要があります。 私は機能onBackPressed() { finish(); }を試しましたが、何も正しく機能しませんでした。バックプレスの前の活動に戻る

MainActivity:

... 
     if(item == shareMenu) { 
      startActivity(new Intent(getBaseContext(), ShareViaBluetoothActivity.class)); 
     } 
... 

ShareViaBluetoothActivity:

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.widget.Toast; 

import java.io.File; 
import java.util.List; 

public class ShareViaBluetoothActivity extends Activity { 

    private static final int DISCOVER_DURATION = 300; 
    private static final int REQUEST_BLU = 1; 

    private FileDialog fileDialog; 

    public File getFile() { 
     return file; 
    } 

    public void setFile(File file) { 
     this.file = file; 
    } 

    private File file; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     File mPath = new File(Environment.getExternalStorageDirectory(), "//DIR//"); 
     fileDialog = new FileDialog(this, mPath); 
     fileDialog.addFileListener(new FileDialog.FileSelectedListener() { 
      public void fileSelected(File file) { 
       Log.d(getClass().getName(), "selected file " + file.toString()); 
       setFile(file); 
       sendViaBluetooth(); 
      } 
     }); 
     fileDialog.showDialog(); 
    } 

    public void sendViaBluetooth() { 

     BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 

     if(btAdapter == null) { 
      Toast.makeText(this, "Bluetooth is not supported on this device!", Toast.LENGTH_LONG).show(); 
     } else { 
      enableBluetooth(); 
     } 
    } 

    public void enableBluetooth() { 

     Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 

     discoveryIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVER_DURATION); 

     startActivityForResult(discoveryIntent, REQUEST_BLU); 
    } 

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

     if(resultCode == DISCOVER_DURATION && requestCode == REQUEST_BLU) { 

      Intent intent = new Intent(); 
      intent.setAction(Intent.ACTION_SEND); 
      intent.setType("*/*"); 

      intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(file.toString()))); 
      intent.setPackage("com.android.bluetooth"); 

      PackageManager pm = getPackageManager(); 
      List<ResolveInfo> appsList = pm.queryIntentActivities(intent, 0); 

      if(appsList.size() > 0) { 
       String packageName = null; 
       String className = null; 
       boolean found = false; 

       for(ResolveInfo info : appsList) { 
        packageName = info.activityInfo.packageName; 
        if(packageName.equals("com.android.bluetooth")) { 
         className = info.activityInfo.name; 
         found = true; 
         break; 
        } 
       } 

       if (!found) { 
        Toast.makeText(this, "Bluetooth havn't been found", 
          Toast.LENGTH_LONG).show(); 
       } else { 
        intent.setClassName(packageName, className); 
        startActivity(intent); 
       } 
      } 
     } else { 
      Toast.makeText(this, "Bluetooth is cancelled", Toast.LENGTH_LONG) 
        .show(); 
     } 
    } 
} 
+0

あなたを助けることができ、より詳細なコードを与えるだろうあなたは正確に私は戻るボタンを押すと、しかし、それは前のアクティビティに来る 'によって何を意味するか説明することができ、それが見えますがクリック可能ではありません(スクリーンのような)私はもう一度戻るボタンを押す必要がありますか? –

+0

MainActivityからアクティビティを開始すると、fileDialogが表示されます。それからキャンセルしたいときは、「戻るボタン」を押します。ダイアログが消え、MainActivityは表示されますが、クリックできません。つまり、私はいつでもどこでも画面上でタップすることができ、何も起こりません。写真のようなそのようなスクリーンショット。もう一度「戻る」ボタンを押すと、すべてが正常です。 –

+0

はい、それを解決する方法は? finish()メソッドを試しましたが、うまくいきません。 –

答えて

0

何を説明することはそれほど完全ではなかったです。しかし、私はこれにいくつかの理由がありますが、これを確認することができます。
1. super.onBackPressed()を使用するか、onBackPressed()を返すことができます。
2.アクティビティ起動モードの動作を確認してください。

しかし、あなたは、より良い、私は

+0

私はすべてを与えたと思います。 :) MainActivityで私はShareViaBluetoothActivityを呼び出し、ShareViaBluetoothActivityコードが掲載されています。 –

+0

ShareViaBluetoothActivityコードが何を意味するのですか? – amyli

+0

私はすでにこのクラスのコードを投稿しています。 :) –

関連する問題