Sorry for causing you guys trouble a lot.
My app has now been completed. The only problem now is that my First time shown welcome screen is where the new file is going to be created. This welcome screen’s sharedpref’s are in the main_activity where the file is also being read.
As a result, it tried to read the file even before it is even created ! So what will be the solution for it?
Code for Main Activity:
package com.omm.easybalancerecharge;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";
EditText num;
Button ch;
TelephonyManager operator;
String opname;
TextView status;
TextView setID;
String myID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
setstatus();
setIDNO();
//Checks and displays Welcome Screen
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref,
false);
if (!welcomeScreenShown) {
Intent welcome = new Intent("android.intent.action.WELCOME");
startActivity(welcome);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit();
}
//Reading Data from the file to be created at welcome screen for the first time.
myID = readData();
recharge();
}
//readData method
protected String readData() {
// TODO Auto-generated method stub
String ret = "";
try {
FileInputStream fIN = openFileInput("iqid");
InputStreamReader in = new InputStreamReader(fIN);
BufferedReader br = new BufferedReader(in);
ret = br.readLine();
} catch (FileNotFoundException e) {
Log.e("ID ACTIVITY", "File Not Found");
} catch (IOException e) {
Log.e("ID ACTIVITY", "Cannot Read From File");
}
return ret;
}
}
Code For The First Time Welcome Screen:
package com.omm.easybalancerecharge;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FirstTime extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_time);
final EditText ID = (EditText) findViewById(R.id.IDNO);
Button save = (Button) findViewById(R.id.sButton);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String myFile = ID.getText().toString();
FileOutputStream fOS = openFileOutput("iqid", Context.MODE_PRIVATE);
fOS.write(myFile.getBytes());
Toast.makeText(getBaseContext(), "ID Saved",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e("Exception", "Save Failed");
} finally {
finish();
}
}
});
}
}
This problem is the only thing preventing my app to run successfully without any FCs.
All permissions are ofcourse set in the manifest.
Edit: And apparently the file isn’t being created at all :/
You should use the startActivityForResult:
In your FirstTime set the data which you want to return back to MainActivity, If you don’t want to return back don’t set any.
eg: In FirstTime if you want to send back data
if you don’t want to return data
Now in your MainActivity class write following code for onActivityResult() method
This should do the trick.