Monday, May 30, 2011

Share in Android

Android provides a nice feature IntentFilter.An application is said to accept share if and only if Activity has IntentFilter ACTION_SEND in Manifest.All the apps such as Twitter,FaceBook,Mail etc.. have ACTION_SEND as Intent Filter in their Manifest Files.


       
Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);  
picMessageIntent.setType("text/*"); 
picMessageIntent.putExtra(Intent.EXTRA_TEXT, "http://www.gpuri.com/images/705/70592.png");
startActivity(Intent.createChooser(picMessageIntent, "Share your picture using:"));

The above code pops a list of apps to recive share event like



Inorder to share to a particular App,First of all Know whether target application is installed in the device using packageManager.

Use following piece,

     
    Intent sendShareIntent = new Intent(Intent.ACTION_SEND);
    PackageManager pm = getPackageManager();
    List activityList = pm.queryIntentActivities(sendShareIntent, 0);
         
    for (int i = 0; i < activityList.size(); i++) { 
        ResolveInfo app = activityList.get(i);
        Log.e("###", "Name: " + app.activityInfo.name);
           
    }
            
      
So,I will check for my FaceBook app installed in my phone or not and i'll directly start facebook share activity using Intent
              
     Intent sendShareIntent = new Intent(Intent.ACTION_SEND);
     sendShareIntent.setClassName("com.facebook.katana", "com.facebook.katana.ShareLinkActivity");
     sendShareIntent.setType("text/*");
     sendShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.google.com/");
     startActivity(sendShareIntent);      
  
This will activate facebook app directly if is present.It would be like


For Twitter the package is "com.twitter.android" and share class is "com.twitter.android.PostActivity".

For all the data like text,image,video just set the type of intent to "text/*" and pass the required url in putExtra(ExtraText).

Currently, In FaceBook we cannot share text.At any cost we have to send only Link URL.Otherwise it will ask for the link to post.

Run On UIThread in Android

To push the piece of code running in a thread to main UI thread ,

MyActivityObject.runOnUiThread(new Runnable() {
       
 @Override
        public void run() {
                //code out here
        }
  });
 
One of the usecase will be, modify an UI element's  data from doinbackground() 
method of Asynctask.

Tuesday, May 10, 2011

Loader for WebView : Android

To set Loader for webview,Set the webviewclient and override OnpageStarted,onPageFinished and onReceivedError.

ProgressDialog _dialog ; //  Global Variable

WebView webView = (WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient(){

   
   @Override
   public void onPageStarted(WebView view, String url, Bitmap favicon) {
        _dialog =ProgressDialog.show(CurrentActivity.this, "", "Please wait...");
    super.onPageStarted(view, url, favicon);
   }  //  end  OnPageStarted

   @Override
   public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    _dialog.dismiss();
   }  //  end OnPageFinished()
   
   @Override
   public void onReceivedError(WebView view, int errorCode,
     String description, String failingUrl) {
    super.onReceivedError(view, errorCode, description, failingUrl);
    try{
     _dialog.dismiss();
    }catch (Exception e) {
     // TODO: handle exception
    }
   }  //  end onReceivedError()
   
  }); // end WebViewclient

When onPageStarted is called ,start the dialog and finish in onPageFinished , onRecievedError() methods.

LWUIT 1.5 and latest Netbeans6.9 for J2Me updates

Attended Oracle developer seminar,Chen revealed some of the latest features of the upcoming LWUIT to be released in a month or so.

Key features:
1)Introduction of GUI builder.
This would minimize the time for UI development.We can do the total page design in Resource Editor.The internal methods of components can be accessed directly from Resource Editor.This will gonna be an amazing feature for developers.
2)Cross platform support
Cross Platfrom support enables to install our build in various devices like nokia,Blackberry etc.For Blackberry we have build the code in a different way.Yet to know.

In latest Netbeans 6.9 we have many features that will be enabled on J2ME integration.They are:
1)On Device Debugging.
Till date there is no onDeviceDebugging for NetBeans.LWUIT 1.5 adds Device Debugging.
2)CPU and N/w Monitor.
The CPU and n/w monitor will trace the amount of time taken to connect,which threads are taking more time etc.
3)List of devices support
An option under Netbeans 6.9 enables us to get the list of devices supported by our Midp . And also we can just check the JSR ,which will give the list of suppoted devices.
4)Different resolutions testing.One solution
The emulator can be maximized ,minimized and change to size what ever you want using cursor.So all resolutions testing can be done very easily.

Saturday, May 7, 2011

Setting Connection timeout using HTTPClient

To provide timeout for HttpClient create a HttpParams Object and set Http timeout and socket timeouts .

Code Snippet
   
HttpParams httpParameters  = new BasicHttpParams();
// Set the timeout in milliseconds 
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);

HttpConnectionParams.setSoTimeout(httpParameters,30000);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

Authentication for Connection,WebView,VideoView

Authentication for Connections in android can be set very easily by just a pice of code:

  
DefaultHttpClient httpClient = new DefaultHttpClient();         
  
httpClient.getCredentialsProvider().setCredentials(
     new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
     new UsernamePasswordCredentials("username", "password"));

//if you dont know which host and port you are going to connect use
// AuthScope.ANY_HOST ,AuthScope.ANY_PORT or else respective values



Authentication for WebView in android can be set using following snippet:
Register a new webclient for webview and override onReceivedHttpAuthRequest method,Set handler parameter the required authentication .

//_webView is object for WebView class
_webView.setWebViewClient(new WebViewClient(){

   @Override
   public void onReceivedHttpAuthRequest(WebView view,
     HttpAuthHandler handler, String host, String realm) {
     handler.proceed("username","password");

   }
  });



Authentication for VideoView:

I didnt get any method for authentication of VideoView but a possible solution might be :

you should provide auth parameters (username/password) inside the URL. Usually this will be accepted (but not necessatilly, you should test):

http://username:password@www.yourhostname.com/whatever



Any suggestions on videoview Auth are welcomed.

Post Code Snippetes in Blogger as you like

Create a Border shapes for Layouts

We can create border for a layout using in Android.

  
    android:right="7dp" android:bottom="7dp" />    

Define this shape xml in drawable and set this shape as background for whatever layout you want.