Search This Blog

Monday 26 August 2013

ShareActionProvider implementation in android newest version

Action Bar comes under standard design pattern of Android. ShareActionProvider is another easy step to implement Action Bar if we need to share some Intent with other application installed in the device.
ShareActionProvider introduce by android team in Android version 4.0 and above and this does not support any backward compatibility.
So lets create one simple demo of ShareActionProvider.

1) Update you menu file to create ShareActionProvider

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
   <item  
     android:id="@+id/menu_item_share"  
     android:actionProviderClass="android.widget.ShareActionProvider"  
     android:showAsAction="ifRoom"  
     android:title="Share"/>  
 </menu>  

2) Update your MainActivity to create ShareActionProvider

 public class MainActivity extends Activity {  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
      }  
      private ShareActionProvider mShareActionProvider;  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate menu resource file.  
           getMenuInflater().inflate(R.menu.main, menu);  
           // Locate MenuItem with ShareActionProvider  
           MenuItem item = menu.findItem(R.id.menu_item_share);  
           // Fetch and store ShareActionProvider  
           mShareActionProvider = (ShareActionProvider) item.getActionProvider();  
           setShareIntent(createShareIntent());  
           // Return true to display menu  
           return true;  
      }  
      // Call to update the share intent  
      private void setShareIntent(Intent shareIntent) {  
           if (mShareActionProvider != null) {  
                mShareActionProvider.setShareIntent(shareIntent);  
           }  
      }  
      private Intent createShareIntent() {  
           Intent shareIntent = new Intent(Intent.ACTION_SEND);  
           shareIntent.setType("text/plain");  
           shareIntent.putExtra(Intent.EXTRA_TEXT,  
                     "http://androidtrainningcenter.blogspot.in");  
           return shareIntent;  
      }  
 }  

3) Let see how we create an intent to share with other application

      private Intent createShareIntent() {  
           Intent shareIntent = new Intent(Intent.ACTION_SEND);  
           shareIntent.setType("text/plain");  
           shareIntent.putExtra(Intent.EXTRA_TEXT,  
                     "http://androidtrainningcenter.blogspot.in");  
           return shareIntent;  
      }  




Source

http://developer.android.com/training/sharing/shareaction.html

Monday 19 August 2013

Getting List of non system application from Android phone

Getting all application list from android phone is quite easy. PackageManger gives the list of all installed application. But main concern remain how to avoid system from the list. I have done this job and prepare a piece of code to implement this functionality in an easiest way.
I keep filter to avoid non system application.

Filter to avoid Non System Application
           private boolean isSystemPackage(ApplicationInfo pkgInfo) {  
                int mask = ApplicationInfo.FLAG_SYSTEM  
                          | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;  
                return (pkgInfo.flags & mask) == 0;  
           }  

GetallApTest .Java will find all system app in background and will give you the list
 import java.util.ArrayList;  
 import java.util.List;  
 import android.app.Activity;  
 import android.app.ProgressDialog;  
 import android.content.pm.ApplicationInfo;  
 import android.content.pm.PackageInfo;  
 import android.content.pm.PackageManager.NameNotFoundException;  
 import android.os.AsyncTask;  
 import android.os.Bundle;  
 import android.util.Log;  
 import com.eurotronik.isenior.myMenu.MyAppData;  
 public class GetallApTest extends Activity {  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           AndroidApplication mAsAndroidApplication = new AndroidApplication();  
           mAsAndroidApplication.execute();  
      }  
      private class AndroidApplication extends  
                AsyncTask<Void, ArrayList<MyAppData>, ArrayList<MyAppData>> {  
           ProgressDialog pd;  
           @Override  
           protected void onPreExecute() {  
                super.onPreExecute();  
                pd = new ProgressDialog(GetallApTest.this);  
                pd.setMessage("Loadin...");  
                pd.setCancelable(false);  
                pd.show();  
           }  
           @Override  
           protected ArrayList<MyAppData> doInBackground(Void... params) {  
                ArrayList<MyAppData> res = new ArrayList<MyAppData>();  
                List<PackageInfo> packs = getPackageManager().getInstalledPackages(  
                          0);  
                for (int i = 0; i < packs.size(); i++) {  
                     PackageInfo p = packs.get(i);  
                     try {  
                          ApplicationInfo ai = getPackageManager()  
                                    .getApplicationInfo(p.packageName, 0);  
                          if ((!isSystemPackage(ai))) {  
                               Log.e("Skip", "App");  
                               continue;  
                          }  
                     } catch (NameNotFoundException e) {  
                          e.printStackTrace();  
                     }  
                     MyAppData newInfo = new MyAppData();  
                     newInfo.setName(p.applicationInfo  
                               .loadLabel(getPackageManager()).toString());  
                     newInfo.setImage(p.applicationInfo  
                               .loadIcon(getPackageManager()));  
                     res.add(newInfo);  
                }  
                return res;  
           }  
           private boolean isSystemPackage(ApplicationInfo pkgInfo) {  
                int mask = ApplicationInfo.FLAG_SYSTEM  
                          | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;  
                return (pkgInfo.flags & mask) == 0;  
           }  
           @Override  
           protected void onProgressUpdate(ArrayList<MyAppData>... values) {  
                super.onProgressUpdate(values);  
                pd.dismiss();  
                /**  
                 * List OF app  
                 */  
                ArrayList<MyAppData> mList = values[0];  
           }  
      }  
 }  
Android News and source code