I'm developing an app that needs to send some sms and at the same time, make a phone call. The principal problem is if I put the code with normal intents that works but make that phone call and it finish intermediately because the phone starts to send the sms.
My question is:
How can I do the two things at the same time? I've just thought about sending the sms in background but I don't know how to.
Java always make the phone call first, also if the code is not in that order.
I resolved this using two services. The first one make the call and the second send the messages. My problem was that i'm using the same button in order to start both so did it in that way. Thanks
`Intent callIntent = new Intent(Intent.ACTION_CALL);`
`callIntent.setData(Uri.parse("tel:" + phoneToCall));`
`callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);`
`this.getApplicationContext().startActivity(callIntent);`
`Timer timerSMS = new Timer();`
`TimerTask tSMS = new TimerTask() {`
`#Override`
`public void run() {`
`SmsManager sms = SmsManager.getDefault();`
`for (int i = 0; i = 10)`
`sms.sendTextMessage(phoneToSMS[i], null, "Hello World", null, null);'
`}`
`}`
`};`
`timerSMS.schedule(tSMS, 1);`
It starts after 1 ms. So we can say that it works almost at the same time.
Related
For now my app have a chat that comunicate via bluetooth with an OBD port in the car.
Now i want upgrade my project for real time information, so i want create a method that repeat some Array with a list of commands and repeat the sendMessage(message) every sec or 500 millisec (something for real time data).
There is some bestway to do that?
I have my Activity with 4 EditText for showing data and a Button with "start scan" and if pressed it becomes a "stop scan" and interrupt the infinite loop of commands.
In the same time i need to take back data and show results in the EditText.
EDIT
Or just use an AlarmManager?
EDIT 2
With this code not work properly because send only the first message after 5 sec and the second it lost...
How can i send all the commands into ArrayList one at a time every t millisec?
public void repeatCommand(){
for (final String command : commandArray){
final Handler handlerTimed = new Handler();
handlerTimed.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
sendMessage(command);
}
}, 5000);
}
/*String message = "010C\r";
sendMessage(message);*/
}
Sorry I didn't write android code for so long but I had your case so long ago.
You have to define a Service and start it in foreground with RETURN_STICKY and then write a handler with timer which execute your code per second (or what you like!). Then you can broadcast your result or how you want to communicate with your activity and use it.
Then start service and stop it with your button.
PS:
1. As far as I know alarmManager is not a good idea in this case.
Somehow you have to be sure that your Service will not be killed by android.
I am writing my first android application and I have a few problems with one activity. I am trying to add tts and timers which will be activated on a button push. I have a few questions on this and will be splitting up my questions into separate threads :). Let me start with the tts issues first.
When I added the tts function onInit it works fine. when I put this is as Random random it read the strings as one long sentence, but it read it. But when I added the tts call to a button, and I need to have 2 strings read,......
Button Start = (Button)findViewById(R.id.btnStart);
Start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Intent myIntent = new Intent(getBaseContext(), MainActivity.class); //put timer start here
// startActivity(myIntent);
tts.speak(voice[2], TextToSpeech.QUEUE_FLUSH, null); //falls through to 4 and timer does not start.
tts.speak("... ",TextToSpeech.QUEUE_FLUSH, null);
tts.speak(voice[0], TextToSpeech.QUEUE_FLUSH, null);
Timer1.start();
//pause here 5 seconds
//Timer2.start();
//pause here 5 minutes
// Timer3.start();
//continue code from here
// startActivity(myIntent);
}
});
.......I had to take out the Intent or it crashes the project. When I do this it falls through and only reads the last string. And when I add the timer calls, which are probably written wrong, it crashes the project. I think I have the timer functions written correctly but that's the next thread. I know I have this written wrong. But I can find nothing anywhere with an example of how this is done.
So what I need help with:
1. getting the button to read several strings from an array with pauses
2. getting the function calls written correctly to activate the timers
I have one basic java programming class I have taken and this is my first android app. Any help would be appreciated.
Use QUEUE_ADD instead of QUEUE_FLUSH.
I wish to achieve a SMS loopback, i.e. to send and receive SMS from the same application. In order to do so, I have created a class that extends BroadcastReciever, implemented the onReceive() method, and declared the relevant permissions.
I verified the implementation by sending a SMS using telnet.
I want to automate the telnet process, i.e. having the application test itself by sending the SMS. In order to do so, I invoke the following method in the main activity, but the BroadcastReceiver is never called:
private final void sendSMS() {
final TelephonyManager telMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
final int len = telMgr.getLine1Number().length();
final String phoneNum = telMgr.getLine1Number().substring(len - 4, len);
final String msg = "msg";
SmsManager.getDefault().sendTextMessage(phoneNum, null, msg, null, null);
}
Any clue what is wrong...?
UPDATE: Note that the code above is intended for the emulator.
Not sure if I understand you question right, but are you trying send an SMS from the emulator to itself? As far as I know, that is not possible. Just load up another emulator, and send messages between them.
Since telnet commands work, your BroadcastReceiver is probably correctly implemented, but you should probably attach the code for it anyways... Its hard to troubleshoot code you can't see :)
My problem is make a function to set up themes in my application every 00:00 AM if there are new themes. As I know, to do this problem we must use a loop.
Here is my code:
private void updateThemes() {
Thread time = new Thread() {
public void run() {
int time = 0;
while(time > 86400000) {
//invoke method or start new activity
}
}
};
}
Please help me - Thanks.
Running a thread and waiting for a full day is not going to work. What if the phone is shutdown? What if the user switches to another app and your app is closed by Android because it needed the resources? Besides, it's not very battery friendly either.
You'd better use the Android AlarmManager to set the times at which you would like to check for updates. Also specify a BroadcastReceiver in your app that will receive and process the alarms. There's an example application that does this here or check this post for more info.
I have been trying to get a bitmap screenshot of a SurfaceView for days but the more I look into it, there doesn't seem to be a solution at present for Android OS 2.3.4 based OSs my device from HTC.
So on to Plan B, where I just found out another blog: "On my HTC Evo 3d, all I have to do is hold the power button for 1-2 sec and then hit the home button and it takes a screen shot. No app required." Turns out this works perfectly on my tablet.
I also know from digging around there are these intents: android.intent.action.SCREEN_OFF & android.intent.category.HOME
(So I tried a bunch of code experiments to try to mimic the 2-key combo in code to get a screenshot in this brute force manor. Unfortunately without success).
So my ? -- Does anyone have any insights into a method to invoke this 'screenshot sequence' for my HTC device from java code? (Presume I need to fool the OS into thinking I am holding down the power key AND tap the Home key, simultaneously)...
More: Here is a snip of the code I am attempting:
Button click for test... ...
Thread t = new Thread() {
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_POWER);
Instrumentation inst2 = new Instrumentation();
inst2.sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
} // run
}; // thread t
Doesnt work as the inst.sendKeyDownUpSync is wrong as I need a sendKeyDown (& hold) behavior or its equivel
Many thanks for any advise. If I do get this working, I will post the solution here. Cheers GH
PS; I presume there is some custom intent under the hood doing this? Is there a system log somewhere to trey to peek at the call tree to find out what it is ?
EDIT (MORE)... 9/24/11
More. Still not working but I am heading down this path & think it is closer...
// Attempt to SIMULATE A Long press (DOWN) + HOME to tell the HTC to invoke the 'Screenshot' command (WARNING: HTC Tablet specific behavior!)
Thread tt = new Thread() {
public void run() {
final KeyEvent dapowerkey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER);
Handler onesecondhandler = new Handler();
onesecondhandler.postDelayed(new Runnable() {
public void run() {
// fpr about 1 second send power down keystrokes (DOWN ONLY)
while (true) { dispatchKeyEvent(dapowerkey); }
} // we are done running on the timer past time point
}, 750); // 3/4 second key press
// send the HOME keystroke
Instrumentation inst1 = new Instrumentation();
inst1.sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
} // outer thread run tp mpt block the GUI
}; // outer thread t
tt.start();
...
Also thought if I can send the right intent directly to the proper place on the device that I might be able to kick off a screen capture function directly (which is what I really want. Through some log examinations (when you Long-Power + Home click on HTC) a program called 'com.htc.mysketcher' (FlashActivity) is being called...
Again, if I figure this out then I will post to the group... Cheers GH