add a number automatically while calling - java

I have searching for days to resolved my issue..
I want my apk to call a number when i click on a button and while calling (after few seconds)I want my app to add automatically another number(like if we open keyboard of the default dialer view and type a number manually)..
Exemple : when I have a call card to call another country, We have to put some specific number to continue ( put 1 to call to France, put 2 to call to Italia...etc )I want to put AUTOMATICALLY number few seconds after the first ring !
There is my code for now :
public void onClick(View v) {
switch (v.getId()) {
case R.id.num5: // When I click on this button
Intent localIntent5 = new Intent(Intent.ACTION_CALL);
localIntent5.setData(Uri.parse("tel:0153204255"));
startActivity(localIntent5);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent localIntent6= new Intent(Intent.ACTION_CALL);
localIntent6.setData(Uri.parse("tel:71-0609472130%23"));
startActivity(localIntent6);
break;
}
}
Thanks for help..
please help me I'm looking for a days !

Use this code on button Click:-
num5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent localIntent5 = new Intent(Intent.ACTION_CALL);
localIntent5.setData(Uri.parse("tel:*****"));
startActivity(localIntent5);
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent localIntent6= new Intent(Intent.ACTION_CALL);
localIntent6.setData(Uri.parse("tel:*******"));
startActivity(localIntent6);
}
});

Related

How to finish a specific intent under different method

So I have two methods. The intent works as a "Night mode" filter for screen. It is called out when button is pressed in 1 method. But I can't figure out how can I finish the intent under the other method when the off button is clicked. Any help is appriciated! I wanna finish the overlay intent which acts as a filter under the "turnNightOff" method.
Heres the code:
private void turnNightOn() {
try {
modeOnOffButton.setImageResource(R.drawable.nightmodeonbutton);
Intent filter = new Intent(this,NightmodeFilter.class);
startActivity(filter);
} catch (Exception e) {
e.printStackTrace();
}
}
private void turnNightOff() {
try {
modeOnOffButton.setImageResource(R.drawable.nightmodeonoffbutton);
} catch (Exception e) {
e.printStackTrace();
}
}

Try Catch Block Not Working Inside OnClickListener() [duplicate]

This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 7 years ago.
I am working on a project to control lights using arduino + ethernet shield + android app. I am using android studio for app development purpose. The issue is, try-catch block has been implemented inside the OnClickListener() which doesnt seem to work. I am new to android app development and cant think of a solution for the same. The app does get installed but the buttons do not perform their function. i.e. the server doesnt receive any package. Actually, initially the targetSdkVersion was set to 8, hence the holo theme and the buttons worked properly. Once i set it to 22 (lollipop) the material theme gets applied by default and the buttons no longer work. Thanking you in advance.
public void led(String s) throws Exception
{
byte[] b=(s.getBytes());
if(isOnline())
{
serverHostname1 = new String ("192.168.1.177");
ip = InetAddress.getByName(serverHostname1);
d1 = new DatagramSocket();//}
try{
send = new DatagramPacket(b,b.length, ip, 8032);
}catch(Exception e){
}
d1.send(send);
d1.setSoTimeout(10000);
d1.receive(rec);
modifiedSentence = new String(rec.getData());
InetAddress returnIPAddress = rec.getAddress();
Toast.makeText(getApplicationContext(),"Reply from Server:"+returnIPAddress,Toast.LENGTH_LONG).show();
d1.close();
}
else
{
Toast.makeText(getApplicationContext(),"No network",Toast.LENGTH_LONG).show();
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button on= (Button)findViewById(R.id.on);
Button off= (Button)findViewById(R.id.off);
on.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
//ArduinoActivity a=new ArduinoActivity();
led("1");
Toast.makeText(getApplicationContext(),"ON",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Error::"+e);
}
}
});
off.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
//ArduinoActivity b=new ArduinoActivity();
led("2");
Toast.makeText(getApplicationContext(), "OFF",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
//TODO Auto-generated catch block
System.out.println("Error::"+e);
}
}
});
}
}
The issue was not with the try-catch block but the networking operations being carried out inside UI thread (sending of packet) which is not allowed in android versions 3.0+ . The solution is to make use of Asynctask for the same. Thank you :)

how to switch to a new activity only by either pressing abutton or waiting for a specific time

From an activity,I want to switch to another activity either by pressing a button or by waiting for some time (For example - 5 seconds). In the code below,if the user pressed the button within less than the specified timing (e.g: 5 seconds), he will be directed to a new activity. In case the user waited (5 seconds) without pressing any thing, the new activity will also be switched to.
The problem is, if the user pressed the button - before 5 seconds- to switch to the new activity, actually he will get what he wants, BUT after (5 seconds) the same activity will be launched again. How to invalidate the timer in case of the user chose to switch to the new activity by pressing the button.
Java Code:
introButton = (Button) findViewById(R.id.introButtonID);
introButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent00 = new Intent(Intro.this, MPL.class);
startActivity(intent00);
finish();
}
});
...
...
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent intent01 = new Intent(Intro.this, MPL.class);
startActivity(intent01);
finish();
}
}, SPLASH_TIME_OUT);
}
Try this code,
//Kept at class level
private Runnable r;
private Handler temp;
introButton = (Button) findViewById(R.id.introButtonID);
introButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity();
}
});
r=new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
startActivity();
}
};
temp=new Handler();
temp.postDelayed(r, SPLASH_TIME_OUT);
}
private void startActivity(){
Intent intent00 = new Intent(Intro.this, MPL.class);
startActivity(intent00);
temp.removeCallbacks(r);
finish();
}
I'm relative new to Android and I suggest to use something like this.
There is no system-based way to do this functionality in a proper way.
I could only imagine to use system-time to implement this.
startInclTimer = starttime + seconds;
while(true){
actualtime = System.currentTimeMillis();
if (startInclTimer < actualtime) {
//start intent
}
}

Android ListView MediaPlayer

I am trying to loop through my listView and hightlight the textviews and play the text being highlighted.
Problem is all the audio files play at a same time and highlight and unhighlight at a same time as well. and the whole app seems to feeeze while the audio gets played..
I know I have to use Threads maybe but still not sure how to this.
Here is my code
//This is how I call it
Player plOptions = new Player(sound,tf);
plOptions.start();
// this is the thread
private class Player extends Thread
{
String sPlayerPath;
TextView tf;
public Player(String sPath_, TextView tv_) {
sPlayerPath = sPath_;
tf= tv_;
}
#Override
public void run()
{
final MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(sPlayerPath);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
TextHighLight(tf);
}
});
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
TextUnHighLight(tf);
}
});
}
}
You should provide the code in onListItemSelected to answer the question why all items are being played,highlighted at the same time.
But what I understand from what you did provide, is that you dont need a thread. just use prepareAsync() instead of prepare(), android will handle the threading for you.This will greatly simplify your code.
Initialize your media player when in create method.Implement onCompletionListener event and provide the method to play the next song.In the next method reset the media player then it will work perfectly.
for eg and some imp note:
If you are clicking in the listview and the media file plays in another activity it will play multiple songs. for eg ActivityA contains ur list and ActivityB contains mediacontrols. In this case it will play multiple song to avoid this situation u must use service

AsyncTask not displaying onPreExecute

I have two AsyncTask Activities, whose sole responsibilties are to execute an AsyncTask on the UI Thread. One of them works great, displays the progressBar, and updates the ProgressBar message. This is for my SearchActivity, which is actually a search activity with the Google Search metadata for declaring a search activity. I created a clone of the AsyncTask, and only changed doInBackground code, and also clone the SearchActivity and called it browseActivity.
I start BrowseActivity for result in a class that extends listView when an item is clicked. What I intended to happen was for the onPreExecute code of the AsyncTask to display a progressBar as the SearchActivity does, between the ListView activity, and the ResultActivity that the ListView will start onActivityResult. The onPreExcute code is being called but the ProgressBar is never displayed. (I thought I ran it on the UI thread correctly?)
Here is BrowseActivity:
AsyncTask<String, String, ArrayList<SearchResult>> browseTask;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String genreExtra = getIntent().getStringExtra(Genre.BUNDLE_TAG);
if(genreExtra!=null){
genre = Genre.getgenreFromExtra(genreExtra);
}
String categoryExtra = getIntent().getStringExtra(Category.BUNDLE_TAG);
if(categoryExtra!=null){
this.category = Category.getCategoryFromExtra(categoryExtra);
}
final Connector connector = GlobalVars.getCurrentConnector();
Thread browseThread = new Thread(){
#Override
public void run() {
try {
browseTask = connector.browse(BrowseActivity.this, category, genre, 1);
browseTask.execute("");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
runOnUiThread(browseThread);
ArrayList<SearchResult> result = null;
try {
result = browseTask.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bundle queryBundle = new Bundle();
queryBundle.putSerializable(SERIALIZABLE_KEY, result);
Intent i = new Intent();
i.putExtra(BUNDLE_KEY, queryBundle); //Put in our browse results
i.putExtra(Category.BUNDLE_TAG, category.putExtra()); //put our category in return intent
i.putExtra(Genre.BUNDLE_TAG, genre.putExtra()); //put our genre in return intent
setResult(RESULT_OK, i);
finish();
}
Here is the CategoryActivity (extends ListActivity) activity where browseActivity is started:
#Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(CategorySelectionView.this, BrowseActivity.class);
final Category[] categories = Category.values();
i.putExtra(Category.BUNDLE_TAG, categories[position].putExtra());
i.putExtra(Genre.BUNDLE_TAG, genre.putExtra());
CategorySelectionView.this.startActivityForResult(i, BrowseActivity.RESULT_REQUEST_CODE);
}
// Listen for results.
protected void onActivityResult(int requestCode, int resultCode, Intent data){
// See which child activity is calling us back.
switch (requestCode) {
case BrowseActivity.RESULT_REQUEST_CODE:
// This is the standard resultCode that is sent back if the
// activity crashed or didn't doesn't supply an explicit result.
if (resultCode == RESULT_CANCELED){
}
else {
ArrayList<SearchResult> recentQueryResults = (ArrayList<SearchResult>)data.getBundleExtra(
BrowseActivity.BUNDLE_KEY).getSerializable(BrowseActivity.SERIALIZABLE_KEY);
GlobalVars.setRecentQueryResults(recentQueryResults);
Category category = Category.getCategoryFromExtra(data.getStringExtra(Category.BUNDLE_TAG));
Intent i = new Intent(CategorySelectionView.this, RomListView.class);
i.putExtra(Category.BUNDLE_TAG, category.putExtra());
i.putExtra(Genre.BUNDLE_TAG, genre.putExtra());
startActivity(i);
}
default:
break;
}
}
Firstly, you can't run an AsyncTask on the main/UI Thread, that's the whole point. When you call browseTask.execute() its going to call its methods on the appropriate Thread (main vs background), regardless of calling runOnUiThread(). By calling runOnUiThread() all you are doing is wrapping the code that starts it in another Runnable. I think is not what you want since after calling runOnUiThread() you try to get the result of the task, which hasn't run yet. The code in onCreate() runs on the UI thread, so this has to finish before something else can run, namely the onPreExecute() of the AsyncTask.
What you should be doing is to create the AsyncTask and put everything you currently have after runOnUiThread() in onPostExecute().

Categories