Monday 12 December 2016

AndroidPhoneCall

Android gives Built-in applications to telephone calls, in a few events we may need to make a telephone call through our application. This should effectively be possible by utilizing verifiable Intent with fitting activities. Likewise, we can utilize PhoneStateListener and Telephony Manager classes, keeping in mind the end goal to screen the adjustments in some communication states on the gadget.

In AndroidMainfest.XML, we should add uses-permission  for Phone Call State as shown below,

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

Activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:textAlignment="center">
 
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android PHONE Call " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="call" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:inputType="phone" 
 android:ems="10" 
 android:layout_marginTop="20dp" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" />
</LinearLayout> 

MainActivity.java :-

package com.kiranapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    Button callbtn;
    EditText ebtn;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 callbtn = (Button) findViewById(R.id.button);
 ebtn = (EditText) findViewById(R.id.editText);
 callbtn.setOnClickListener(new View.OnClickListener() {
 
@Override 
 public void onClick(View v) {
 String s1 = ebtn.getText().toString();
 Intent callIntent = new Intent(Intent.ACTION_CALL);
 callIntent.setData(Uri.parse("tel:" + s1));
 if (ActivityCompat.checkSelfPermission(MainActivity.this,
         Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling  // 
 ActivityCompat#requestPermissions 
 // here to request the missing permissions, and then overriding // 
 public void onRequestPermissionsResult(int requestCode, String[] permissions,
 // int[] grantResults) 
 // to handle the case where the user grants the permission. See the documentation 
 // for ActivityCompat#requestPermissions for more details. //
 return;
 }
 startActivity(callIntent);

 }
 });

 }

 }

Output :-

Android phone call
Android PhoneCall figure

Sunday 11 December 2016

Android Wifi

Android WiFi manager can manage WiFi connectivity. And  it can be used as enable WiFi , disable WiFi and also add network or scanning for access points.

In Android manifest.XML we should add the "uses-permission". i.e shown below,

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> 

Activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:textAlignment="center">
 
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android Wifi " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Enable Wifi" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Disable Wifi" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button1" 
 android:layout_gravity="center_horizontal" />

</LinearLayout>


MainActivity.java :-

package com.kiranapp;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button enablebtn,disablebtn;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 enablebtn = (Button) findViewById(R.id.button);
 disablebtn = (Button) findViewById(R.id.button1);
 enablebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(true);

 }
 });
 disablebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(false);
 }
 });
 }
 }

Output :-

Android wifi disable mode
Android WiFi fig :1







             Android WiFi Disable mode












Android wifi enable mode
Android WiFi fig :2



                Android WiFi Enable mode

SpeechToText

 Android Speech To Text its similar to Android Text To Speech, but here in this code reverse to text to speech and shown example code below.

Activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:textAlignment="center">
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android SpeechToText " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="speak" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<EditText 
 android:layout_width="124dp" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" 
 android:hint="input" />

</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Button speak;
    TextView input;
@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 speak=(Button)findViewById(R.id.button);
 input=(TextView)findViewById(R.id.textView);
 speak.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,"en_US");
 try {
 startActivityForResult(intent,1);
 }
 catch (Exception e) {
 e.printStackTrace();
 Toast.makeText(MainActivity.this, "Dude Your phone does not 
                            support Speech to Text",Toast.LENGTH_SHORT).show();
 }
 }
 });
 }

@Override 
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if(requestCode==1){
 if(resultCode==RESULT_OK&&data!=null) {
 ArrayList<String> al=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
 input.setText(al.get(0));
 }
 }
 super.onActivityResult(requestCode, resultCode, data);
 }
 }

Output :-

Android Speech To Text
Android Speech To Text

TextToSpeech

 Android is providing Text To Speech and its speaks many different languages with different names, country or others.

Activiy_main.xml :-

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:textAlignment="center">


<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="speak" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<EditText
 android:layout_width="124dp" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" 
 android:hint="input" />

</LinearLayout>

MainActivity.java :- 

package com.kiranapp;

import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
Button speakbtn;
EditText input;
TextToSpeech tts;

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 speakbtn=(Button)findViewById(R.id.button);
 input=(EditText)findViewById(R.id.editText);
 tts=new TextToSpeech(getApplicationContext(), new 
                                  TextToSpeech.OnInitListener() {
@Override 
 public void onInit(int status) {
 Toast.makeText(MainActivity.this, "Text to Speech engine is Intialized",
                                Toast.LENGTH_SHORT).show();
 }
 });
 speakbtn.setOnClickListener(new 
                              View.OnClickListener() {
@Override public void onClick(View v) {
 String name=input.getText().toString();
 String s1="Android";
 tts.speak(s1,RESULT_OK,null);
 tts.setLanguage(Locale.UK);
 }
 });
 }
}

Output :-

Android Text To Speech
Android Text To Speech