I'm developing DES decryption in Android platform.
this is my main
package com.example.crack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class Main extends Activity {
public final static String EXTRA_MESSAGE = "com.example.crack.MESSAGE";
public final static String EXTRA_PLAINTEXT = "com.example.crack.PLAINTEXT";
public final static int ENCRYPTION_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendMessage(View view) {
Intent intent = new Intent(this, encryption.class);
EditText editText = (EditText) findViewById(R.id.input_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivityForResult(intent, ENCRYPTION_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == ENCRYPTION_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
String result = data.getStringExtra(encryption.EXTRA_ENCRYPTION_RETURN);
Intent intent = new Intent(this, DisplayMessage.class);
intent.putExtra(EXTRA_MESSAGE, result);
startActivity(intent);
}
}
}
}
and this is the partial of my encrpytion
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.encryption);
Intent intent = getIntent();
message = intent.getStringExtra(Main.EXTRA_MESSAGE);
//Dictionary
is = getResources().openRawResource(R.raw.english);
in = new BufferedReader(new InputStreamReader(is));
readDic();
String result = "";
try {
result = decryptBruteForce();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_ENCRYPTION_RETURN,result);
setResult(RESULT_OK,returnIntent);
finish();
}
when i click on the button, it calls the sendMessage function, while it is running the decryption the screen just black out until it finish running.
I had try using progress bar follow this guide, but not working, I need a button that can stop the process while running.
And is it possible to set a log on view, which show what the function is doing right now? like what is shown in the IDE log? Example, showing what key is the decryption trying right now.
Or maybe just a progress bar or please wait will do too.
I tried to change the sendMessage to this, yet it still black out and crash
public void sendMessage(View view) {
final Intent intent = new Intent(this, encryption.class);
view.setEnabled(false);
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
pd = new ProgressDialog(context);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
#Override
protected Void doInBackground(Void... arg0) {
try {
//Do something...
EditText editText = (EditText) findViewById(R.id.input_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivityForResult(intent, ENCRYPTION_REQUEST);
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
if (pd!=null) {
pd.dismiss();
b.setEnabled(true);
}
}
};
task.execute((Void[])null);
}
if I put sleep to 50000, it did not crash, but still it black out.
You can do it with a Thread and a Handler. While you try each combination, you update the progress bar.
private int mProgressStatus = 0;
private Handler mHandler = new Handler();
protected void onCreate(Bundle savedInstanceState)
{
.... // Other initializations
mProgress = (ProgressBar) findViewById(R.id.progress_bar);
mProgress.setMax(dictionaryLength);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
for (int i=0 ; i<dictionaryLength ; i++)
{
mProgressStatus = decryptBruteForce(i);
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
}
However i recommend you to use AsyncTask to do background operations while you need to update the UI to show the progress or info about whats going on.
http://developer.android.com/intl/es/reference/android/os/AsyncTask.html
Its a good habit to add a cancel control in your loop, so you can finish it from outside of the AsyncTask (for example another button in you UI).
private class DecryptTask extends AsyncTask<String, Integer, Long> {
protected Long doInBackground(String... words)
{
long wordsDecrypted = 0;
for (int i = 0; i < words.length ; i++) {
wordsDecrypted += decryptBruteForce(i);
publishProgress(i);
// Escape early if cancel() is called
if (isCancelled())
break;
}
return wordsDecrypted;
}
protected void onProgressUpdate(Integer... progress) {
mProgress.setProgress(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Decrypted " + result + " words");
}
}
And you can cancel the AsyncTask from outside with the cancel method:
http://developer.android.com/intl/es/reference/android/os/AsyncTask.html#cancel(boolean)
PD: Codes are not tested, just examples to show how it works
Related
public class gameHomeScreen extends Activity implements View.OnClickListener{
public static Socket socket;
private Button button;
private PrintWriter printwriter;
private BufferedReader bufferedReader;
String serverReply = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game_home_screen);
if(login.client!=null){
socket = login.client;
}else if(signup.client!=null){
socket = signup.client;
}
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
public void onBackPressed() {
moveTaskToBack(true);
}
#Override
public void onClick(View v) {
button.setText("Waiting...");
SendMessage sm = new SendMessage();
sm.execute();
while(!serverReply.equals("game_found")) {
//do nothing
}
Intent i = new Intent(this, InGame.class);
startActivity(i);
finish();
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
printwriter = new PrintWriter(socket.getOutputStream(), true);
printwriter.println("game_queue"); // write the message to output stream
InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
do {
serverReply = bufferedReader.readLine();
}while(!serverReply.equals("game_found"));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}
}
This line of code doesnt work :
button.setText("Waiting...");
I've tried using handlers and using the UIThread but nothing works. The text only changes for half a second to "waiting..." whenever a game is found though, its pretty strange...
You're hanging the UI thread:
while(!serverReply.equals("game_found")) {
//do nothing
}
Don't do this on the onClick method. This method is called from the UI thread and you're locking it up (which might also give you an ANR).
Remove the loop from onClick:
#Override
public void onClick(View v) {
button.setText("Waiting...");
SendMessage sm = new SendMessage();
sm.execute();
//while(!serverReply.equals("game_found")) {
// do nothing
//}
//Intent i = new Intent(this, InGame.class);
//startActivity(i);
//finish();
}
And start the intent in your AsyncTask:
private class SendMessage extends AsyncTask<Void, Void, Void> {
// ...
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Intent i = new Intent(this, InGame.class);
startActivity(i);
finish();
}
}
When the user clik on Register Button, a Custom Dialog box appear and the user move to the menu. But in my case, the Costum Dialog appear just for one second and then the user pass to the next layout.
How to set a time for the Custom Dialog please ?
Here my code :
buttonRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new Thread(new Runnable() {
#Override
public void run() {
final String name = inputName.getText().toString();
final String mail = inputEmail.getText().toString();
final String password = inputPassword.getText()
.toString();
PatientFunctions patientFunction = new PatientFunctions();
json = patientFunction.registerPatient(name, mail,
password);
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res) == 1) {
Intent main = new Intent(
getApplicationContext(), Main.class);
main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
finish();
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
CustomizeDialog customizeDialog = new CustomizeDialog(RegisterPatient.this);
customizeDialog.show();
Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
customizeDialog.cancel();
customizeDialog.dismiss();
}
}, 3000);
}
});
There are many good options including AlarmManager, Timer & TimerTask
You can use a handler too like so:
Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
customdialog.cancel();
customdialog.dismiss();
}
}, 500);
the best way to do these kind of process is to use AsyncTask class
and override onPreExecute and doInBackground and onPostExecute
see the official guide
implement the doInBackground() callback method, which runs in a pool of background threads. To update your UI, you should implement onPostExecute(), which delivers the result from doInBackground() and runs in the UI thread, so you can safely update your UI. You can then run the task by calling execute() from the UI thread
private class Registration extends AsyncTask<String, Void, String>{
CustomizeDialog customizeDialog;
#Override
protected void onPreExecute() {
// show ur dialog
customizeDialog = new CustomizeDialog(RegisterPatient.this);
customizeDialog.show();
}
#Override
protected String doInBackground(String... params) {
final String name = params[0];
final String email = params[1];
final String password = params[2];
PatientFunctions patientFunction = new PatientFunctions();
json = patientFunction.registerPatient(name, mail,
password);
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
return res;
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
}
#Override
protected void onPostExecute(String result) {
if (Integer.parseInt(res) == 1) {
if(customizeDialog != null)
customizeDialog.dismiss();
Intent main = new Intent(
getApplicationContext(), Main.class);
main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//move to the next activity
startActivity(main);
finish();
}
}
}
Edit
and then execute it what ever you want
buttonRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final String name = inputName.getText().toString();
final String mail = inputEmail.getText().toString();
final String password = inputPassword.getText()
.toString();
new Registration().execute(name,mail,password);
}
});
I have an app that generates music after a user authenticates with OAuth on a webview activity, looking something like this: main player activity-OAuth Activity-back to main player activity. However, the onCreate method is being called twice when going from the OAuth activity, resulting in two audio tracks generated and played at the same time.
Here's part of the code from the MainActivity:
public class MainActivity extends Activity {
int pitch=60;
private static final float VISUALIZER_HEIGHT_DIP = 50f;
Random rn;
boolean isRunning = true;
boolean isPlaying=false;
SeekBar fSlider;
double sliderval;
MediaPlayer mediaPlayer=new MediaPlayer();
ImageButton startStopButton;
ImageButton stopButton;
SeekBar vSlider;
VisualizerView mVisualizerView;
private Visualizer mVisualizer;
ImageButton connectButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// point the slider to the GUI widget
rn = new Random();
fSlider = (SeekBar) findViewById(R.id.frequency);
fSlider.setProgress(0);
vSlider= (SeekBar) findViewById(R.id.seekBar2);
vSlider.setMax(10);
vSlider.setProgress(0);
TextView viewinterval=(TextView) findViewById(R.id.textView2);
viewinterval.setText("");
startStopButton=(ImageButton) findViewById(R.id.imageButton2);
View activity= this.findViewById(R.id.playerActivity);
stopButton=(ImageButton) findViewById(R.id.imageButton1);
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int)(VISUALIZER_HEIGHT_DIP * getResources().getDisplayMetrics().density));
params.addRule(RelativeLayout.BELOW, R.id.seekBar2);
mVisualizerView = new VisualizerView(this);
mVisualizerView.setLayoutParams(params);
((ViewGroup) activity).addView(mVisualizerView);
connectButton=(ImageButton) findViewById(R.id.imageButton3);
connectButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
mediaPlayer.pause();
Intent intent= new Intent(getApplicationContext(), WebViewActivity.class);
startActivity(intent);
}
});
if(riskscores.length !=0){
viewinterval.setText("generating audio");
new MIDISequence().execute();
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected void onPause() {
super.onPause();
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
class MIDISequence extends AsyncTask<String,Void,String>{
Here's the code from my OAuth Activity
public class WebViewActivity extends Activity {
private WebView gWebView;
final String REDIRECT_URI = "https://localhost:5000/receive_code";
final String CLIENT_ID = "can't post it here";
final String CLIENT_SECRET = "can't post it here";
final String SCOPE = "basic names genomes analyses";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
gWebView = (WebView) findViewById(R.id.webView1);
gWebView.loadUrl("https://api.23andme.com/authorize/?redirect_uri="
+ REDIRECT_URI + "&response_type=code&client_id=" + CLIENT_ID
+ "&scope=" + SCOPE);
Log.d("WEBVIEW", "got to webpage");
gWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (url.startsWith(REDIRECT_URI)) {
Log.d("WEBVIEW", "onpagefinished is called");
System.out.println("got to override");
if (url.indexOf("code=") != -1) {
//if the query contains code
String queryString = null;
try {
queryString = new URL(url).getQuery();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(queryString);
String[] params = queryString.split("&");
String code = null;
for (String param : params) {
if (param.startsWith("code=")) {
code = param.substring(param.indexOf('=') + 1);
}
}
gWebView.setVisibility(View.GONE);
new PostRequest().execute(code);
// don't go to redirectUri
}
}
}
});
}
class PostRequest extends AsyncTask<String,Void,String>{
#Override
protected String doInBackground(String... params) {
code retrieving client data.....
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
System.out.println("CPE" + e);
} catch(SocketException ex)
{
Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
return "error occured";
} catch (JSONException e) {
e.printStackTrace();
return "error occured";
} catch (IllegalStateException e) {
e.printStackTrace();
return "error occured";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "error occured";
}
}
return "request complete";
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("Post result", result);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
}
The onCreate method of the MainActivity is called twice for some reason... What is going on here?
There seems to be a mistake in your implementation. The thing is, you are trying to use an Intent object to navigate back to your MainActivity form WebActvitity. This is a problem. You shouldn't be doing that.
Whenever you wanna move back to your previous activity, you should simply be calling finish() in the current Activity.
In our scenario,the by using Intent in your WebActivity you are creating a new instance for your MainActivity which already exists in the stack(background). Simply calling finish() in the WebActivity should close it and your MainActivity should be visible.
Do the following changes,
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("Post result", result);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
Replace the above method like this,
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("Post result", result);
finish();
}
Other than the expected cases, I have observed that only those activities (onCreate) are called twice which are creating new Thread or Runnable, AsyncTask in your case. (I believe this to be a bug in Android).
The solution is simple (though you may not like it :p)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
if(savedInstanceState == null){
// everything else that doesn't update UI
}
}
It seems you are getting multiple instance of your first activity. use this in manifest of 1st activity:
android:launchMode="singleTop"
else call finish() after doing startActivity() for 2nd activity
i've an progress circle that is set inside an AsyncTask. It shows for about a second as the asynctask is executing, then disappears. once the task is completed if i press the back button the circle shows for a long time. why is this?
private class AsyncGetRota extends AsyncTask<String, Void, Void> {
ProgressDialog progressDialog;
#Override
protected void onPreExecute()
{
progressDialog= ProgressDialog.show(NfcscannerActivity.this,
"Connecting to Server"," retrieving rota...", true);
//do initialization of required objects objects here
};
#Override
protected Void doInBackground(String... params) {
try {
Log.e(TAG, "inside doInBackground");
rotaArray = nfcscannerapplication.loginWebservice.getRota(params[0], params[1]);
cancel(true);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
progressDialog.dismiss();
};
}
[update]
getRota.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.e(TAG, "onclicked getRota");
String[] params = new String[]{"36", "18-09-2012"};
AsyncGetRota agr = new AsyncGetRota();
agr.execute(params);
for(int i = 0; i < 60; i++){
if(agr.isCancelled() == true){
Log.e(TAG, "asyncTask is finished");
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}//end of for loop
Intent intent = new Intent(NfcscannerActivity.this,
GetRota.class);
Bundle b = new Bundle();
b.putSerializable("rotaArray", rotaArray);
intent.putExtra("rotaArrayBundle", b);
startActivity(intent);
}// end of onclick
});
...
new MyAsyncTask().execute(string);
...
}
class MyAsyncTask extends AsyncTask<String, Void, Whatever > {
...
#Override
protected Whatever doInBackground(String... params) {
Log.e(TAG, "inside doInBackground");
rotaArray = nfcscannerapplication.loginWebservice.getRota(params[0], params[1]);
return rotaArray;
}
#Override
protected void onPostExecute(Whatever result)
{
super.onPostExecute(result);
if(progressDialog != null)
progressDialog.dismiss();
Intent intent = new Intent(NfcscannerActivity.this, GetRota.class);
Bundle b = new Bundle();
b.putSerializable("rotaArray", result);
intent.putExtra("rotaArrayBundle", b);
startActivity(intent);
}
}
You should let the execution continue after you start the AsyncTask, and not block it using some loop or something..
try to implement it like this:
protected void onPreExecute() {
dialog = new ProgressDialog(activity);
dialog.setMessage("Processing...");
dialog.show();
}
protected void onPostExecute(Void result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
};
that's always works for me
Couple of problems here, you do not initialize ProgressDialog, initialize a constructor that initializes you ProgressDialog like this...
public AsyncGetRota(Activity activity) {
this.activity = activity;
dialog = new ProgressDialog(activity);
}
Then in onPostExecute check if your ProgressDialog is null, like this
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
if(progressDialog != null)
progressDialog.dismiss();
}
My Question
How to show a progress bar in android while waiting to receive an SMS?
The dialog should fulfill the following conditions
The dialog should be dismissed in maximum for 5 minutes.
The dialog should be dismissed if SMS is received before 5 minutes.
What I tried
public class GetSMS extends Activity {
...
...
private boolean progressDialogFlag;
private ProgressDialog progressDialog;
...
...
new SMSReceiver().execute();
}
public class SMSReceiver extends AsyncTask<Void, Integer, Void> {
protected void onPreExecute() {
// start a progressdialog box
super.onPreExecute();
progressDialogFlag = true;
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Waiting to Receive SMS...");
progressDialog.setCancelable(false);
progressDialog.show();
}
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
if (progressDialogFlag == true) {
progressDialog.setProgress(progress[0]);
}
if (progress[0] == 100 || progressDialogFlag == false) {
progressDialog.dismiss();
}
}
protected void onPostExecute(Void result) {
}
#Override
protected Void doInBackground(Void... params) {
int interval = 2;
int totalDuration = 5 * 60 * 1000; // 5 min
int sleepPeriod = (totalDuration / 100) * interval;
for (int i = 2; i <= 100; i += 2) {
publishProgress(i);
try {
Thread.sleep(sleepPeriod);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
}
smsReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
progressdialog = false;
}
}
What are my doubts
I think the use of progressDialogFlag to dismiss the progressDialog when an Sms is received before 5 min is causing the problem.
The use of variable progressDialogFlag in UI and worker thread is causing some kind of race condition.
Thanks for help.
can try some thing like below when you start the progressDialog
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(null!=progressDialog && progressDialog.isShowing()){
// try{
progressDialog.dismiss();
}
}
}, 5*60*1000);
but need some more checks......
Do like this:
private ProgressDialog p;
private void sendSMS(){
p = new ProgressDialog(Context);
registerReceiver(broadcastReceiver, SMSINTENT);
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
p.dismiss();
getActivity().unregisterReceiver(this);
}
};
Don't know if it is the best solution, but it works!