Sie sind auf Seite 1von 7

Android Application Development Training Tutorial

For more info visit http://www.zybotech.in

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Tutorial : Create Wi-Fi Hotspot to Share Internet Connection

I believe a lot of us would have experienced the inconvenience of searching high and low for internet access on our laptops or wireless devices outside our workplace/home only to realize that the only internet access readily available is the wireless 3G connection on our mobile phone. 3G connectivity might not be as fast or stable as Wi-Fi but it does the job for casual browsing and checking of emails. Prior to the release of Froyo, there are Android apps such as PDAnet that can provide the wireless tethering function on your Android but with the release of Froyo, this capability has been inbuilt into the OS itself. Creating a Wifi hotspot to share your 3G connectivity is not as hard as it sounds to be. Step 1: Verify that your phone is running Android 2.2 Press the Menu button > Settings > Scroll to bottom and tap on About Phone. You will see the number under Android version and it should show 2.2 if you are running Froyo. Step 2: Setup your Android Once you have confirmed that the phone is running Android 2.2, tap the Back button > Scroll to the top of the screen > Tap Wireless and Network button. You will see an option called Tethering and Portable Hotspot. Tap on it and tick the option called Portable Wifi hotspot. It's as simple as that! Now, you will get a notification that your phone has been configured to act as a hotspot for any wireless devices in range of your phone. Step 3: Security For security reasons, you might want to restrict access to your Android hotspot and it's possible to do that by adding WPA2 PSK encryption to it. To do that, go back to the Wireless and Network menu, choose Tethering and Portable option and tap the Portable Wi-fi hotspot Settings button. Tap the Configure Wi-Fi hotspot button and under Security, choose the WPA2 PSK option. Then, you will be
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

asked to enter a password with exactly 8 characters. Tap Save and your network is now secured. Step 4: Unique SSID You may also wish to rename your Android wireless hotspot to another unique name of your choice at the same menu where you choose the Security option in Step 3 (Configure Wi-Fi hotspot menu). Step 5: Connectivity now requires password Any wireless devices that wish to connect to your Android hotspot now will need to connect with your password of choice. This will ensure that no one else is using your network bandwidth allocation. Step 6: Wired Connectivity as an Alternative You can also choose to share your 3G connectivity via USB. Plug your Android into a computer/laptop using a USB cable > Tap on Tethering and Portable hotspot menu > Tap on USB Tethering option. For a demo of the wireless and wired tethering option available in Android 2.2, you might want to take a look at the video shown below.

Using WiFi API


Android comes with a complete support for the WiFi connectivity. The main component is the system-provided WiFiManager. As usual, we obtain it via getSystemServices() call to the current context. Once we have the WiFiManager, we can ask it for the current WIFi connection in form of WiFiInfo object. We can also ask for all the currently available networks via getConfiguredNetworks(). That gives us the list of WifiConfigurations. In this example we are also registering a broadcast receiver to perform the scan for new networks.

WiFiDemo.java Code:
package com.example; import java.util.List; import import import import import import import import import import import import import android.app.Activity; android.content.BroadcastReceiver; android.content.Context; android.content.IntentFilter; android.net.wifi.WifiConfiguration; android.net.wifi.WifiInfo; android.net.wifi.WifiManager; android.os.Bundle; android.util.Log; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.TextView;

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

import android.widget.Toast; public class WiFiDemo extends Activity implements OnClickListener { private static final String TAG = "WiFiDemo"; WifiManager wifi; BroadcastReceiver receiver; TextView textStatus; Button buttonScan; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Setup UI textStatus = (TextView) findViewById(R.id.textStatus); buttonScan = (Button) findViewById(R.id.buttonScan); buttonScan.setOnClickListener(this); // Setup WiFi wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); // Get WiFi status WifiInfo info = wifi.getConnectionInfo(); textStatus.append("\n\nWiFi Status: " + info.toString()); // List available networks List<WifiConfiguration> configs = wifi.getConfiguredNetworks(); for (WifiConfiguration config : configs) { textStatus.append("\n\n" + config.toString()); } // Register Broadcast Receiver if (receiver == null) receiver = new WiFiScanReceiver(this); registerReceiver(receiver, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); Log.d(TAG, "onCreate()"); } @Override public void onStop() { unregisterReceiver(receiver); } public void onClick(View view) { Toast.makeText(this, "On Click Clicked. Toast to that!!!", Toast.LENGTH_LONG).show(); if (view.getId() == R.id.buttonScan) { Log.d(TAG, "onClick() wifi.startScan()"); wifi.startScan(); } } }

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

The WiFiScanReceiver is registered by WiFiDemo as a broadcast receiver to be invoked by the system when new WiFi scan results are available. WiFiScanReceiver gets the callback via onReceive(). It gets the new scan result from the intent that activated it and compares it to the best known signal provider. It then outputs the new best network via a Toast. WiFiScanReceiver.java Code:
package com.example; import java.util.List; import import import import import import import android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.net.wifi.ScanResult; android.net.wifi.WifiManager; android.util.Log; android.widget.Toast;

public class WiFiScanReceiver extends BroadcastReceiver { private static final String TAG = "WiFiScanReceiver"; WiFiDemo wifiDemo; public WiFiScanReceiver(WiFiDemo wifiDemo) { super(); this.wifiDemo = wifiDemo; } @Override public void onReceive(Context c, Intent intent) { List<ScanResult> results = wifiDemo.wifi.getScanResults(); ScanResult bestSignal = null; for (ScanResult result : results) { if (bestSignal == null || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0) bestSignal = result; } String message = String.format("%s networks found. %s is the strongest.", results.size(), bestSignal.SSID); Toast.makeText(wifiDemo, message, Toast.LENGTH_LONG).show(); Log.d(TAG, "onReceive() message: " + message); } }

The layout file for this example is fairly simple. It has one TextView wrapped in a ScrollView for scrolling purposes. /res/layout/main.xml Code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonScan" android:text="Scan"></Button> <ScrollView android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textStatus" android:text="WiFiDemo" /> </ScrollView> </LinearLayout>

For the AndroidManifest.xml file, just remember to add the permissions to use WiFi: Code:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

Android Audio Recording Tutorial


After many hours of trying to record audio on my Google Android device, Ive finally arrived at a workable solution. There were a few bumps along the way besides the horribly out of date MediaRecorder documentation, which was sorely lacking details. For one, I could only get audio to record to the SD card. Additionally, the directory being recorded to must already exist before attempting to record to it. Without further ado, here is a complete example for recording audio on the Android via the MediaRecorder API:
package com.benmccann.android.hello; import java.io.File; import java.io.IOException; import android.media.MediaRecorder; import android.os.Environment; /** * @author <a href="http://www.benmccann.com">Ben McCann</a> */ public class AudioRecorder { final MediaRecorder recorder = new MediaRecorder(); final String path; /** * Creates a new audio recording at the given path (relative to root of SD card). */ public AudioRecorder(String path) { this.path = sanitizePath(path); }

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

private String sanitizePath(String path) { if (!path.startsWith("/")) { path = "/" + path; } if (!path.contains(".")) { path += ".3gp"; } return Environment.getExternalStorageDirectory().getAbsolutePath() + path; } /** * Starts a new recording. */ public void start() throws IOException { String state = android.os.Environment.getExternalStorageState(); if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) { throw new IOException("SD Card is not mounted. It is " + state + "."); } // make sure the directory we plan to store the recording in exists File directory = new File(path).getParentFile(); if (!directory.exists() && !directory.mkdirs()) { throw new IOException("Path to file could not be created."); } recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(path); recorder.prepare(); recorder.start(); } /** * Stops a recording that has been previously started. */ public void stop() throws IOException { recorder.stop(); recorder.release(); } }

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Das könnte Ihnen auch gefallen