Created
September 14, 2012 11:02
-
-
Save Matsushige/3721314 to your computer and use it in GitHub Desktop.
CSVファイルダウンロード<アクティビティ>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import android.app.Activity; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class DownloadCSVActivity extends Activity { | |
Button downloadButton; | |
Button databaseButton; | |
TextView textView; | |
String[] cont = new String[6]; | |
DownloadBroadcastReceiver progressReceiver; | |
IntentFilter intentFilter; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
downloadButton = (Button) findViewById(R.id.button1); | |
databaseButton = (Button) findViewById(R.id.button2); | |
textView = (TextView) findViewById(R.id.textView1); | |
downloadButton.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
Intent intent = new Intent(getBaseContext(), | |
DownloadService.class); | |
intent.putExtra("url", | |
"/**対象のURL*/"); | |
startService(intent); | |
}// onClick | |
}); | |
databaseButton.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
accessDatabase(); | |
textView.setText(DatabaseOpe.userInfo); | |
}// onClick | |
}); | |
registerDownloadBroadcastReceiver(); | |
}// onCreate | |
public void onDestroy(){ | |
super.onDestroy(); | |
unregisterReceiver(progressReceiver); | |
}// onDestroy | |
protected void registerDownloadBroadcastReceiver() { | |
progressReceiver = new DownloadBroadcastReceiver(); | |
intentFilter = new IntentFilter(); | |
intentFilter.addAction("DOWNLOAD_ACTION"); | |
registerReceiver(progressReceiver, intentFilter); | |
}// registerDownloadBroadcastReceiver | |
class DownloadBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Bundle bundle = intent.getExtras(); | |
String fileName = bundle.getString("fileName"); | |
try { | |
InputStream in = openFileInput(fileName); | |
if (in != null) { | |
InputStreamReader isRea = new InputStreamReader(in); | |
BufferedReader bRea = new BufferedReader(isRea); | |
String str; | |
deleteData(); | |
while ((str = bRea.readLine()) != null) { | |
String[] str2 = str.split(","); | |
for (int i = 0; i < 6; i++) { | |
cont[i] = str2[i]; | |
if(cont[i].contains("\"")){ | |
cont[i] = cont[i].replace("\"", ""); | |
}//if | |
}// for | |
renewDatabase(cont[0], cont[1], cont[2], cont[3], cont[4], cont[5]); | |
}// while | |
in.close(); | |
}// if | |
Toast.makeText(getApplicationContext(), "登録完了", Toast.LENGTH_SHORT).show(); | |
} catch (Throwable t) { | |
Toast.makeText(getApplicationContext(), | |
"Exception:" + t.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
}// onReceive | |
}// DownloadProgressBroadcastReceiver | |
private void accessDatabase(){ | |
DatabaseOpe.read(this); | |
}// accessDatabase | |
private void renewDatabase(String _id, String type, String id, String name, String code, String time ){ | |
DatabaseOpe.write(this, _id, type, id, name, code, time); | |
}// renewDatabase | |
private void deleteData(){ | |
DatabaseOpe.delete(this); | |
}// deleteData | |
}// Activity |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment