我们在android studio中创建了三个按钮(即开始、停止扫描和插入),用于wifi扫描。按下“开始”按钮时,将发出“wifi扫描已启动”通知,应用程序将扫描所有wifi。经过一定时间的wifi扫描后,按下“停止扫描”按钮,另一个通知出现“wifi扫描停止”。最后,按下“插入”按钮将数据保存到手机中。然而,当我按下“插入”按钮时,出现的不是“数据保存…”消息,而是“停止扫描”。wifi扫描仍在进行,没有停止。当我检查csv时。数据在外部存储器中,它显示0字节的数据。文件夹已创建,但没有数据,可能有什么问题?
Button startWifiScan = (Button) findViewById(R.id.startbutton);
startWifiScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText = (EditText) findViewById(R.id.groundTruth);
CeLLiD = editText.getText().toString();
if(CeLLiD.equals(""))
{
alert("Ground Truth is not Set");
}
else if(ismeasure == true)
alert("Scanning is already started!");
else {
Toast.makeText(getApplicationContext(), "Wifi Scan started", Toast.LENGTH_SHORT).show();
rssDatabase = dumpDataToHashmap(1);
sdDatabase = dumpDataToHashmap(0);
onResume();
ismeasure = true;
isRecord = false;
}}
});
Button stopWifiScan = (Button) findViewById(R.id.stopbutton);
stopWifiScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Wifi Scan stopped", Toast.LENGTH_SHORT).show();
if(ismeasure == false) alert("Start Scanning!");
else {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
File folder = new File(Environment.getExternalStorageDirectory() + "/Testing" + hour + "-" + minute + "-" + second+":"+CeLLiD);
folder.mkdir();
String csv = folder.getAbsolutePath() + "/data.csv";
try { file_writer = new FileWriter(csv, true);
} catch (IOException e) {
e.printStackTrace();
}
onStop(); }
}
});
Button insert= (Button) findViewById(R.id.insertbutton);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(ismeasure == true)
alert("Stop Scanning!");
else if(ismeasure == false && isRecord == true) alert("Start Scanning!");
else {
Toast.makeText(getApplicationContext(), "Data Saving.....", Toast.LENGTH_SHORT).show();
try {
writeToCsv();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
查看xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="387dp"
android:layout_height="456dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="3dp">
<TableLayout
android:id="@+id/wifiTable"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="368dp"
android:layout_height="79dp"
android:layout_marginBottom="32dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
app:layout_constraintVertical_bias="0.857"
tools:ignore="UnknownId">
<EditText
android:id="@+id/groundTruth"
android:layout_width="109dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:ems="8"
android:hint=" x , y "
android:inputType="textPersonName" />
<Button
android:id="@+id/startbutton"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_below="@+id/groundTruth"
android:layout_marginTop="-4dp"
android:text="Start" />
<Button
android:id="@+id/stopbutton"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_below="@+id/groundTruth"
android:layout_centerHorizontal="true"
android:layout_marginTop="-3dp"
android:text="STOP SCAN" />
<Button
android:id="@+id/insertbutton"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/stopbutton"
android:layout_marginEnd="26dp"
android:layout_marginRight="26dp"
android:text="Insert" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
1条答案
按热度按时间dddzy1tm1#
我无法从您共享的代码片段中看到太多内容;但当您按下停止按钮时,似乎忘记设置ismeasure和isrecord变量。在代码中,您已经进行了验证,以确保用户将始终开始->停止->插入,并且不会跳过任何步骤。但是要检查的变量并不是全部到位,我自己更改了一些变量名;我们的结局是:
现在的问题是,我希望正确传递csv文件名/路径,以便在按insert键后,程序可以正确找到文件并用数据填充;我看到有一个file_writer变量,但除了“writetocsv”函数之外,我没有看到它被使用;所以一定要记住这一点。