Tuesday, May 29, 2012

Android Turn ON wifi programmatically

Android Turn ON wifi  programmatically

Below I added coding for Enable WIFI programmatically.

when start the application wifi also enable.

Activity.class

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class WifiEnableActivity extends Activity {
    /** Called when the activity is first created. */
    private WifiManager wifiManager; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
       TextView text = (TextView)findViewById(R.id.wifiState);
          wifiManager.setWifiEnabled(true); 
          if(wifiManager.isWifiEnabled()){
              text.setText("Wifi State Enabled");
          }
        
    }
}

Then  some permission and user feature required in manifest file.
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 
    <uses-feature android:name="android.hardware.wifi.direct" />
    <uses-permission    android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>  

You Can Download Source Code Here


Android Widget

Android Widget

android widget it's like small application place to your home screen.
You can display any live updates, or messages etc..

XML CODE

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="1000"
    android:initialLayout="@layout/main">
</appwidget-provider>

Screen Shot



WidgetProvider.Class

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
public class WidgetProvider extends AppWidgetProvider {
  DateFormat df = new SimpleDateFormat("hh:mm:ss");
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));
    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < N; i++) {
      int appWidgetId = appWidgetIds[i];
      // Create an Intent to launch ExampleActivity
      Intent intent = new Intent(context, AndroidMyFirstWidgetActivity.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
      // Get the layout for the App Widget and attach an on-click listener
      // to the button
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
     
     // views.setOnClickPendingIntent(R.id.widget1label, pendingIntent);
      // To update a label
     
      views.setTextViewText(R.id.widget1label, "Welcome to Mad Blog : " +df.format(new Date()));
      // Tell the AppWidgetManager to perform an update on the current app
      // widget
      appWidgetManager.updateAppWidget(appWidgetId, views);
    }
  }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.widget"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidMyFirstWidgetActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <!-- <action android:name="android.intent.action.MAIN" />
 -->
               <!--  <category android:name="android.intent.category.LAUNCHER" /> -->
            </intent-filter>
        </activity>
        <receiver android:name=".WidgetProvider" android:label="Mad BLOG Widget">
  <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  </intent-filter>
  <meta-data android:name="android.appwidget.provider" android:resource="@layout/widgetprovider" />
</receiver>
       
    </application>

</manifest>


You Can download Source code HERE

Monday, May 21, 2012

Pull to Refresh ListView in Android.

Pull to Refresh ListView in Android.
It will allow to user pulling down  and releasing from the top of the list.
Then come to basic coding part PulltoRefresh.
Here you can fine some better example for pulltorefresh. https://github.com/chrisbanes/Android-PullToRefresh
Custem ListView
pull_to_refresh_listview.xml
<?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"
    android:layout_height="fill_parent"
    >
       <com.pulltorefresh.mad.PullToRefreshView
        xmlns:ptr="http://schemas.android.com/apk/res/com.pulltorefresh.mad"
        android:id="@+id/pull_refresh_list"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        ptr:ptrMode="both"
       
        />
</LinearLayout>


Screen Shot





















Activty code

package com.pulltorefresh.mad;

import java.util.Arrays;
import java.util.LinkedList;

import com.pulltorefresh.mad.PullToRefreshBase.Mode;
import com.pulltorefresh.mad.PullToRefreshBase.OnRefreshListener;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class PullToRefreshListActivity extends ListActivity {

static final int MENU_MANUAL_REFRESH = 0;
static final int MENU_DISABLE_SCROLL = 1;
static final int MENU_SET_MODE = 2;

private LinkedList<String> mListItems;
private PullToRefreshView mPullRefreshListView;
private ArrayAdapter<String> mAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pull_to_refresh_listview);

mPullRefreshListView = (PullToRefreshView) findViewById(R.id.pull_refresh_list);

// Set a listener to be invoked when the list should be refreshed.
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
mPullRefreshListView.setLastUpdatedLabel(DateUtils.formatDateTime(getApplicationContext(),
System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL));

// Do work to refresh the list here.
new GetDataTask().execute();
}
});

ListView actualListView = mPullRefreshListView.getRefreshableView();

mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));

mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

// You can also just use setListAdapter(mAdapter)
actualListView.setAdapter(mAdapter);
}

private class GetDataTask extends AsyncTask<Void, Void, String[]> {

@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return mStrings;
}

@Override
protected void onPostExecute(String[] result) {
mListItems.addFirst("Added after refresh..New Version Coming soon J-elly Bean");
mAdapter.notifyDataSetChanged();

// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();

super.onPostExecute(result);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_MANUAL_REFRESH, 0, "Manual Refresh");
menu.add(0, MENU_DISABLE_SCROLL, 1,
mPullRefreshListView.isDisableScrollingWhileRefreshing() ? "Enable Scrolling while Refreshing"
: "Disable Scrolling while Refreshing");
menu.add(0, MENU_SET_MODE, 0,
mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
: "Change to MODE_PULL_BOTH");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem disableItem = menu.findItem(MENU_DISABLE_SCROLL);
disableItem
.setTitle(mPullRefreshListView.isDisableScrollingWhileRefreshing() ? "Enable Scrolling while Refreshing"
: "Disable Scrolling while Refreshing");

MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
setModeItem.setTitle(mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
: "Change to MODE_PULL_BOTH");

return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case MENU_MANUAL_REFRESH:
new GetDataTask().execute();
mPullRefreshListView.setRefreshing(false);
break;
case MENU_DISABLE_SCROLL:
mPullRefreshListView.setDisableScrollingWhileRefreshing(!mPullRefreshListView
.isDisableScrollingWhileRefreshing());
break;
case MENU_SET_MODE:
mPullRefreshListView
.setMode(mPullRefreshListView.getMode() == Mode.BOTH ? Mode.PULL_DOWN_TO_REFRESH
: Mode.BOTH);
break;
}

return super.onOptionsItemSelected(item);
}

private String[] mStrings = { "A-stro", "B-ender", "C-upcake", "E-clair", "F-royo",
"G-ingerbread", "H-oneycomb", "I-ce Cream Sandwich ","Android Versions From iamvijayakumar.blogspot.com" };
}


   

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...