Learn Python


Click to Learn Python3

Monday, March 14, 2011

Make a phone call using Android code in application?

We can write code to make a call from the android application that we created.

The below code is enough to make a call from the application,


Java

Intent sIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:919894643826"));
startActivity(sIntent);


We can put this code in a click event, so that on clicking a button or textview the call be done.

Likewise we need to add this line inside the Manifest.xml file


XML

<uses-permission android:name="android.permission.CALL_PHONE"></uses>


The java code will be like this

Caller.java

package com.phone.calller;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Callers extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Phone = (Button) findViewById(R.id.call);
Phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sIntent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:919894643826"));
startActivity(sIntent);
}
});
}
}


and the manifest.xml file will be like this


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phone.calller"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Callers"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-permission android:name="android.permission.CALL_PHONE" />

</manifest>

Have a good day.

The full source code

Download source

Monday, March 7, 2011

How to run a Runnable thread or UI thread in Android?

On the launching time of the application itselef the system creates a thread called "main" in the application. This main thread is also called as UI thread, it will only interact with the components of Android UI toolkit.

Here is an example for using the runnable thread in a click event, while clicking the image will be loaded, as it will take time to load the image, we can use the thread as shown below :

Java

public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
final Bitmap bm = loadImage();
mImage.post(new Runnable() {
public void run() {
mImage.setImageBitmap(bm);
}
});
}
}).start();
}



Using Handler in Runnable thread


Here we are using the runnable thread using the handler. When we send a message to the Handler it will get saved into a queue and get executed by the UI thread as soon as possible.



Java

public class ProgressThread extends Activity implements Runnable {
private String result;
private TextView showResult;
private ProgressDialog progress;
@Override
public void onCreate(Bundle savedBundleInstance) {
super.onCreate(savedBundleInstance);
setContentView(R.layout.main);
showResult = (TextView) this.findViewById(R.id.main);
showResult.setText("Press any key to start calculation");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
progress = ProgressDialog.show(this, "Working..", "Calculation is going on...", true,
false);
Thread thread = new Thread(this);
thread.start();
return super.onKeyDown(keyCode, keyEvent);
}
public void run() {
result = MyClass.calculate(800).toString();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
progress.dismiss();
showResult.setText(result);
}
};
}


The runnable thread can be called like this way too


Java

final Runnable runner = new Runnable()
{
public void run()
{
tv.append("Hello World");
handler.postDelayed(this, 1000);
}
};

handler.postDelayed(runner, 1000);


Otherwise we can use the original runnable thread that we are using in the java


Java

Thread thread = new Thread()
{
@Override
public void run() {
try {
while(true) {
sleep(1000);
handler.post(runner);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};

thread.start();


Have a good day.