Friday, February 27, 2015

ANDROID code to read a text file from a raw folder

1)
InputStream is = getResources().openRawResource(R.raw.data);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
while (line != null) {
     line + = line;
}
System.out.println(line);

2)
Scanner scan = new Scanner(getResources().openRawResource(R.raw.data));
sc.useDelimiter(",");
try {
    while (scan.hasNext()) {
        String word = scan.next();       
    }
} finally {
    scan.close();
}

Wednesday, February 25, 2015

ANDROID code to show Notification



    int NOTIF_ID = 9;

     protected void showNotification(String Spelling) {
        
        Context context = getApplicationContext();
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
       
        long when = System.currentTimeMillis();
        Notification notification = new Notification(R.drawable.genera36, "Times to read Spellings", when);
   
        Intent notificationIntent = new Intent(this, TodaysSpellingsActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context,"Spelling: "+Spelling, "",contentIntent);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        nm.notify(NOTIF_ID++, notification);
    }

ANDROID code to create Shortcut Icon of App

Add Permisssion
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />   

private void createShortcut() {
        Log.d(CLASS, "Method called createShortcut()");

        SharedPreferences prefs = getApplicationContext().getSharedPreferences(
                "shortcut_created", 0);
        SharedPreferences.Editor editor = prefs.edit();
        if (!prefs.getBoolean("shortcut_created", false)) {
            Intent shortcutIntent = new Intent(getApplicationContext(),
                    WelcomeActivity.class);
            shortcutIntent.setAction(Intent.ACTION_MAIN);
            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "rVoc");
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(
                            getApplicationContext(),
                            R.drawable.rvoc_img));
            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            getApplicationContext().sendBroadcast(addIntent);
            editor.putBoolean("shortcut_created", true);
            editor.commit();
            Log.e(CLASS, "Short Created Successfully");
        } else {
            Log.e(CLASS, "Shorrt Cut not created");
        }
    }

ANDROID code Hide Soft Keyboard

        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);