-
Notifications
You must be signed in to change notification settings - Fork 11
/
Demo1Activity.java
104 lines (91 loc) · 3.3 KB
/
Demo1Activity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.duke.fileselector;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.duke.dfileselector.activity.DefaultSelectorActivity;
import com.duke.dfileselector.util.FileSelectorUtils;
import java.util.ArrayList;
/**
* 使用默认的Activity启动选择器
*/
public class Demo1Activity extends AppCompatActivity implements View.OnClickListener {
private TextView selectFIle;
public static final String TAG = "MainActivityTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo1);
selectFIle = findViewById(R.id.textview_select_file);
selectFIle.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == null) {
return;
}
int id = v.getId();
if (id == R.id.textview_select_file) {
DefaultSelectorActivity.startActivity(this);//包含广播
// DefaultSelectorActivity.startActivityForResult(this);
}
}
// @Override
// protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == Activity.RESULT_OK && requestCode == DefaultSelectorActivity.FILE_SELECT_REQUEST_CODE) {
// printData(DefaultSelectorActivity.getDataFromIntent(data));
// }
// }
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (context == null || intent == null) {
return;
}
if (DefaultSelectorActivity.FILE_SELECT_ACTION.equals(intent.getAction())) {
printData(DefaultSelectorActivity.getDataFromIntent(intent));
Demo1Activity.this.finish();
}
}
};
private boolean isRegister;
private IntentFilter intentFilter = new IntentFilter(DefaultSelectorActivity.FILE_SELECT_ACTION);
@Override
protected void onResume() {
super.onResume();
if (!isRegister) {
registerReceiver(receiver, intentFilter);
isRegister = true;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (isRegister) {
unregisterReceiver(receiver);
isRegister = false;
}
}
private void printData(ArrayList<String> list) {
if (FileSelectorUtils.isEmpty(list)) {
return;
}
int size = list.size();
Log.v(TAG, "获取到数据-开始 size = " + size);
StringBuffer stringBuffer = new StringBuffer("选中的文件:\r\n");
for (int i = 0; i < size; i++) {
Log.v(TAG, (i + 1) + " = " + list.get(i));
stringBuffer.append(list.get(i));
stringBuffer.append("\r\n");
}
Toast.makeText(this, stringBuffer.toString(), Toast.LENGTH_SHORT).show();
Log.v(TAG, "获取到数据-结束");
}
}