Source Code Simple Android Ball Game

Introduction

This is a tutorial for developing a Ball Game in Android OS. It will introduce you to developing Android games using AndEngine (free Android 2D open source game engine). To understand this tutorial, you will need to have some basic knowledge about AndEngine and programming in Java. If you don't have knowledge about AndEngine, you should check this link. The goal of this tutorial is to teach you how to make your own Android game.

How to connecting to Google Drive from an Android App

This sample demonstrates how to connect to an individual’s Google Drive* online storage service from an Android application. I’ll be describing the sections of code that are executed based on the usage flow of the app. This application is based on the Google Drive sample located here. Before you start building this sample, please follow the instructions in the previous link for registering your application with Google Cloud Console.
Like most Android applications, this one begins in the onCreate function of MainActivity. In this function, you can see the credentials for connecting to the Google Drive account are set up and an activity is started to prompt the user to select their Google account from the registered accounts on the device, or to enter a new one.
mCredential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE));
        startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);

Blocking Incoming call - Android

Step 1:

Create Broadcast receiver class for incoming call

package com.javaorigin.android.sample;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneCallReceiver extends BroadcastReceiver {
 Context context = null;
 private static final String TAG = "Phone call";
 private ITelephony telephonyService;

 @Override
 public void onReceive(Context context, Intent intent) {
  Log.v(TAG, "Receving....");
  TelephonyManager telephony = (TelephonyManager) 
  context.getSystemService(Context.TELEPHONY_SERVICE);  
  try {
   Class c = Class.forName(telephony.getClass().getName());
   Method m = c.getDeclaredMethod("getITelephony");
   m.setAccessible(true);
   telephonyService = (ITelephony) m.invoke(telephony);
   //telephonyService.silenceRinger();
   telephonyService.endCall();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
 }

 
}


Android Audio Demo - AudioTrack , AudioRecord - (Echo Sample)

Simple echo application for how to use android AudioTrack and AudioRecord classes 

Download Source 
Step 1
    Create MainActivity class for audio record and play

package com.javaorigin.audio;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
 AudioManager am = null;
 AudioRecord record =null;
 AudioTrack track =null;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  setVolumeControlStream(AudioManager.MODE_IN_COMMUNICATION);
  init();
  (new Thread() {
   @Override
   public void run() {
    recordAndPlay();
   }
  }).start();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 private void init() {
  int min = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
  record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, 8000, AudioFormat.CHANNEL_IN_MONO,
    AudioFormat.ENCODING_PCM_16BIT, min);

  int maxJitter = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 8000, AudioFormat.CHANNEL_OUT_MONO,
    AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);
 }

 private void recordAndPlay() {
  short[] lin = new short[1024];
  int num = 0;
  am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
  am.setMode(AudioManager.MODE_IN_COMMUNICATION);
  record.startRecording();
  track.play();
  while (true) {
   num = record.read(lin, 0, 1024);
   track.write(lin, 0, num);
  }
 }
 
 boolean isSpeaker = false;

 public void modeChange(View view) {
  Button modeBtn=(Button) findViewById(R.id.modeBtn);
  if (isSpeaker == true) {   
   am.setSpeakerphoneOn(false);
   isSpeaker = false;
   modeBtn.setText("Call Mode");
  } else {   
   am.setSpeakerphoneOn(true);
   isSpeaker = true;
   modeBtn.setText("Speaker Mode");
  }
 }
    
 boolean isPlaying=true;
 public void play(View view){
  Button playBtn=(Button) findViewById(R.id.playBtn);
  if(isPlaying){
   record.stop();
   track.pause();
   isPlaying=false;
   playBtn.setText("Play");
  }else{
   record.startRecording();
   track.play();
   isPlaying=true;
   playBtn.setText("Pause");
  }
 }
 
}

Android Device/Battery Temperature Sensor Sample

Create Temperature activity and create instance for android.intent.action.BATTERY_CHANGED intend  and register with Temperature Broadcast Receiver

Download Source 
package com.javaorigin.temperature;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class TemperatureActivity extends Activity   {
 TemperatureReceiver receiver=new TemperatureReceiver(this);
 TextView tempDisplay=null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
   tempDisplay=(TextView) findViewById(R.id.tempDisplay);

   IntentFilter localIntentFilter = new IntentFilter();
   localIntentFilter.addAction("android.intent.action.BATTERY_CHANGED");
   registerReceiver(receiver, localIntentFilter);   
 }


}