I have a code that reads my inbox and stores each read SMS as a separate file in a fixed folder which i need to upload on FTP later on. I am using the intent for FTP Upload. My program stucture is something like: onCreate() -> Method1 inbox read -> Delete inbox messages -> Method2 Upload FTP -> Method3 Delete Uploaded Folder -> Further Tasks
The problem is that Further Tasks
are called before the app is done uploading the folder contents and the server is simply disconnected. I tried calling Method3
in the Further Tasks
with the help of a Handler
set at delay of 10 minutes but it didn't help as upload may take a lot more time than that also it may not have any files at all to upload so those 10 minutes are wasted. I want the app to wait till the upload is complete. So the question is: What is the proper way of doing this?
EDIT :
The code i am using:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ReadnDeleteSMS();
}
public void FTPUpload(){ //FTPUpload via ANDFTP app
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
Uri ftpUri = Uri.parse("Server"); //Server Call
intent.setDataAndType(ftpUri, "vnd.android.cursor.dir/lysesoft.andftp.uri");
intent.putExtra("command_type", "upload");
intent.putExtra("ftp_username", "username");
intent.putExtra("ftp_password", "password");
intent.putExtra("ftp_pasv", "true");
intent.putExtra("ftp_resume", "true");
intent.putExtra("ftp_encoding", "UTF-8");
intent.putExtra("progress_title", "Uploading folder ...");
intent.putExtra("local_file1", "/sdcard/ReceivedSMS");
intent.putExtra("remote_folder", "remote folder");
intent.putExtra("close_ui", "true"); //Finally start the Activity
startActivityForResult(intent, RESULT_OK); i++;
customHandler.postDelayed(finalizer, 10*60*1000);}
public void ReadnDeleteSMS(){ //Reads, BackUps and Deletes Inbox Messages
Cursor cursor1 = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
if (cursor1.getCount() > 0) {
while (cursor1.moveToNext()){
int id = cursor1.getInt(0);
String address = cursor1.getString(cursor1.getColumnIndex("address"));
String date = cursor1.getString(cursor1.getColumnIndex("date"));
String SMSDate = DateConversion(date);
String msg = cursor1.getString(cursor1.getColumnIndex("body"));
ReadSMS = address + "\n" + SMSDate + "\n" + msg + "\n";
FileName(zText);
myDate = zText.toString(); zText = new StringBuilder();
fileWrite("/sdcard/ReceivedSMS/" + myDate, ReadSMS);
try{
getApplicationContext().getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
Toast.makeText(getBaseContext(), "Successfully Deleted", Toast.LENGTH_SHORT).show();}
catch(Exception e){
Toast.makeText(getBaseContext(), "Error Deleting", Toast.LENGTH_SHORT).show();}}}
FTPUpload();}
public String DateConversion (String date){ //Proper Date Format Display
Long timestamp = Long.parseLong(date);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
return calendar.getTime().toString();}
public void FileName(StringBuilder zText) { //Inbox Message File Name
SimpleDateFormat mSDF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
myDate = mSDF.format(new Date());
myDate = myDate.replace(" ", "");
myDate = myDate.replace("/", "");
myDate = myDate.replace(":", "");
myDate = myDate.replace(".", "");
zText.append(myDate);}
public Runnable finalizer = new Runnable(){ //Main Handler
public void run(){
if (i > 0)
{DeleteDirectory("/sdcard/ReceivedSMS"); i = 0;}
//Some Further Tasks
}
These further tasks are to be called often but if the upload is under execution, no such tasks must perform. The tasks include reading a webpage, String editing and such. These are the main tasks of my app.