|
1 2 3 4 5 |
try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + twitter_user_name))); }catch (Exception e) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + twitter_user_name))); } |
Author Archive
Open twitter via Intent
Friday, May 31st, 2013 at
9:11 am
Comments Off
Set a custom font to entire layout
Friday, May 31st, 2013 at
8:41 am
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
protected void changeFonts(ViewGroup root) { Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/comicsans.ttf"); for(int i = 0; i <root.getChildCount(); i++) { View v = root.getChildAt(i); if(v instanceof TextView ) { ((TextView)v).setTypeface(tf); } else if(v instanceof Button) { ((Button)v).setTypeface(tf); } else if(v instanceof EditText) { ((EditText)v).setTypeface(tf); } else if(v instanceof ViewGroup) { changeFonts((ViewGroup)v); } } } |
Downloading an HTTP file to SDcard with progress notification
Friday, May 31st, 2013 at
8:34 am
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
try { //set the download URL, a url that points to a file on the internet //this is the file to be downloaded URL url = new URL("http://somewhere.com/some/webhosted/file"); //create the new connection HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //set up some things on the connection urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); //and connect! urlConnection.connect(); //set the path where we want to save the file //in this case, going to save it on the root directory of the //sd card. File SDCardRoot = Environment.getExternalStorageDirectory(); //create a new file, specifying the path, and the filename //which we want to save the file as. File file = new File(SDCardRoot,"somefile.ext"); //this will be used to write the downloaded data into the file we created FileOutputStream fileOutput = new FileOutputStream(file); //this will be used in reading the data from the internet InputStream inputStream = urlConnection.getInputStream(); //this is the total size of the file int totalSize = urlConnection.getContentLength(); //variable to store total downloaded bytes int downloadedSize = 0; //create a buffer... byte[] buffer = new byte[1024]; int bufferLength = 0; //used to store a temporary size of the buffer //now, read through the input buffer and write the contents to the file while ( (bufferLength = inputStream.read(buffer)) > 0 ) { //add the data in the buffer to the file in the file output stream (the file on the sd card fileOutput.write(buffer, 0, bufferLength); //add up the size so we know how much is downloaded downloadedSize += bufferLength; //this is where you would do something to report the prgress, like this maybe updateProgress(downloadedSize, totalSize); } //close the output stream when done fileOutput.close(); //catch some possible errors... } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } |
Open file with default application using Intents
Thursday, May 30th, 2013 at
12:17 pm
This code allows you to open a audio and video file with the default application, specifying the MIME.
|
1 2 3 4 5 6 7 8 9 10 |
Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File("/sdcard/test.mp4"); intent.setDataAndType(Uri.fromFile(file), "video/*"); startActivity(intent); Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File("/sdcard/test.mp3"); intent.setDataAndType(Uri.fromFile(file), "audio/*"); startActivity(intent); |
Autostart an application at bootup
Thursday, May 30th, 2013 at
12:13 pm
In AndroidManifest.xml
|
1 2 3 4 5 6 7 8 9 10 |
<receiver android:enabled="true" android:name=".BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> [..] <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> [..] |
BootUpReceiver.class
|
1 2 3 4 5 6 7 8 |
public class BootUpReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Intent i = new Intent(context, MyActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } |
Post a tweet on Twitter
Tuesday, April 23rd, 2013 at
9:43 am
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
public class TwitterClient extends Activity { static final int GET_LOGIN_INFORMATION = 1; WebView webView; RequestQueue requestQueue; String authInfo; /** * Called with the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); // Set the initial text webView = (WebView) findViewById(R.id.webView); webView.loadData( "Please click on setup and enter your twitter credentials", "text/html", "utf-8"); // When they click on the set up button show the login screen Button button = (Button) findViewById(R.id.setup); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(TwitterClient.this, TwitterLogin.class); startSubActivity(intent, GET_LOGIN_INFORMATION); } }); // When they click on the Tweet! button, then get the // text in the edit box and send it to twitter final Activity activity = this; Button button2 = (Button) findViewById(R.id.update); button2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.i("http", "Update clicked"); Map headers = new HashMap(); if (authInfo == null) { return; } headers.put("Authorization", "Basic " + new String(Base64.encodeBase64(authInfo.getBytes()))); EditText user = (EditText) findViewById(R.id.updateText); String text = null; try { text = "status=" + URLEncoder.encode(user.getText().toString(), "UTF-8"); Log.i("http", "with " + text); } catch (UnsupportedEncodingException e) { Log.e("http", e.getMessage()); } byte[] bytes = text.getBytes(); ByteArrayInputStream baos = new ByteArrayInputStream(bytes); // See Twitter API documentation for more information // http://groups.google.com/group/twitter-development-talk/web/api-documentation requestQueue.queueRequest( "https://twitter.com/statuses/update.xml", "POST", headers, new MyEventHandler2(activity), baos, bytes.length, false); } }); // Start a thread to update the tweets from friends every minute requestQueue = new RequestQueue(this); Thread t = new Thread(new MyRunnable(this)); t.start(); } protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) { if (requestCode == GET_LOGIN_INFORMATION && resultCode == RESULT_OK) { // Save the user login information authInfo = data; } } } |
Get My Phone Number
Tuesday, April 23rd, 2013 at
9:41 am
It needs a permission so add the line uses-permission android:name=”android.permission.READ_PHONE_STATE” to the manifest.xml
|
1 2 3 4 5 6 7 8 9 10 11 |
private String getMyPhoneNumber(){ TelephonyManager mTelephonyMgr; mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); return mTelephonyMgr.getLine1Number(); } private String getMy10DigitPhoneNumber(){ String s = getMyPhoneNumber(); return s.substring(2); } |
Latitude and Longitude of a place
Tuesday, April 23rd, 2013 at
9:33 am
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//---geo-coding--- Geocoder geoCoder = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = geoCoder.getFromLocationName( “empire state building”, 5); String add = “”; if (addresses.size() > 0) { p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6)); mc.animateTo(p); mapView.invalidate(); } } catch (IOException e) { e.printStackTrace(); } |
List all music files in android device
Tuesday, April 23rd, 2013 at
9:31 am
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//Some audio may be explicitly marked as not being music String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; String[] projection = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION }; cursor = this.managedQuery( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null); private List<String> songs = new ArrayList<String>(); while(cursor.moveToNext()){ songs.add(cursor.getString(0) + "||" + cursor.getString(1) + "||" + cursor.getString(2) + "||" + cursor.getString(3) + "||" + cursor.getString(4) + "||" + cursor.getString(5)); } |
Vibrate the phone for a given time or a certain pattern
Friday, March 15th, 2013 at
4:43 am
|
1 2 3 4 5 6 7 8 9 |
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 1000 milliseconds long milliseconds = 1000; v.vibrate(milliseconds); // Vibrate in a Pattern with 500ms ON and 500ms OFF for 5 times long[] pattern = { 500, 300 }; v.vibrate(pattern, 5); |
Incoming search terms:
- vibrate HTML5 sample



