Archive

Archive for July, 2012

Jelly Bean source code prepared to release within today


Seem like Google has decided to release the JellyBean v4.1 earlier than expected in mid-July.

http://rootzwiki.com/news/_/articles/announcements/jelly-bean-source-released-r955

Just wait and be ready to get new chewy source code: http://source.android.com/source/downloading.html

Advertisements
Categories: Of Diary Tags: , , ,

Launch Home Chooser Dialog


After long time digging in Android source code, I’ve found a way to pop-up the Home Chooser dialog.

Home Chooser Dialog

Home Chooser Dialog

Above is how it displays on my phone. Anyway, here the way how to do it:

    static final String HOME_CHOOSER_PACKAGE_NAME = "android";
    static final String HOME_CHOOSER_CLASS_NAME = "com.android.internal.app.ResolverActivity";

    private void launchHomeChooser() {
    	Intent i = new Intent(Intent.ACTION_MAIN);
    	i.addCategory(Intent.CATEGORY_HOME);
    	i.setClassName(HOME_CHOOSER_PACKAGE_NAME, HOME_CHOOSER_CLASS_NAME);
    	startActivity(i);
    }

If you search over Internet, there might be many other ways to create a similar Home Chooser dialog, however, it takes lots of coding and customization. Well, I prefer to call the framework one.

One more thing, it works on my device Galaxy SII, running ROM Resurrection Remix v2.5.3 (Android ICS v4.0.4); so I’m not really certain that it would work for every device, but it should work for most of devices then. It runs fine on Android 2.3.x devices, too.

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , ,

Get date time in custom format


Basically, I use [Date] class to get current time and use [SimpleDateFormat] to format my own output.

Just a sample format:

private String getDateTime() {
    // get date time in custom format
    SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
    return sdf.format(new Date(appInfo.firstInstallTime));
}

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , ,

Launch Youtube app to play video

July 7, 2012 1 comment

A simple snippet for launching Youtube app to play video.

	// constant value of package & class name of YouTube app
	public static final String YOUTUBE_PACKAGE_NAME = "com.google.android.youtube";
	public static final String YOUTUBE_CLASS_NAME = "com.google.android.youtube.WatchActivity";
	
	/**
	 * Launch Youtube to watch an URL
	 * @param context
	 * @param url
	 */
	public static void launchYoutube(Context context, String url) {
		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));        
        intent.setClassName(YOUTUBE_PACKAGE_NAME, YOUTUBE_CLASS_NAME);        
        context.startActivity(intent);
	}

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: ,

Create a Service that does a schedule task

July 7, 2012 16 comments

Sometimes you need to do a schedule task for a fixed rate (repeatedly), like [check for emails], [check for battery], [update new Facebook status], etc…

The tip here is to use Service and implement a Timer running a TimerTask for an interval.

My sample here is that I like to get Date and Time in every 10 seconds and display a Toast notification to users.

Here how it looks:

Sample Schedule Task

Sample Schedule Task

So I create my own service called TimeService:

public class TimeService extends Service {
	// constant
	public static final long NOTIFY_INTERVAL = 10 * 1000; // 10 seconds

	// run on another Thread to avoid crash
	private Handler mHandler = new Handler();
	// timer handling
	private Timer mTimer = null;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		// cancel if already existed
		if(mTimer != null) {
			mTimer.cancel();
		} else {
			// recreate new
			mTimer = new Timer();
		}
		// schedule task
		mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
	}

	class TimeDisplayTimerTask extends TimerTask {

		@Override
		public void run() {
			// run on another thread
			mHandler.post(new Runnable() {

				@Override
				public void run() {
					// display toast
					Toast.makeText(getApplicationContext(), getDateTime(),
							Toast.LENGTH_SHORT).show();
				}

			});
		}

		private String getDateTime() {
			// get date time in custom format
			SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
			return sdf.format(new Date());
		}

	}

It’s easy to understand, isn’t it?

Next is to register the TimerService to AndroidManifest for identification.

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".TimeService"/>
    </application>

Our last step is to give a call for starting our service when activity executes.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, TimeService.class));
    }

Finally, just run on the phone and see the result.
The implementation is pretty straightforward and simple. You can freely use this way to create some schedule tasks for your applications.

Cheers,
Pete Houston

Categories: Tutorials Tags: , ,

Error creating project in ADT rev.20

July 6, 2012 2 comments

Now this is what happen after updating ADT to rev.20.

Error create project in ADT rev.20

Error create project in ADT rev.20

 

The problem is that the ADT looks for the Android Support Library in <ANDROID-SDK>\extras\android\support , but in facts, it stays in <ANDROID-SDK>\extras\android\compatibility

So the solution is to copy the folder [\compatibility] and rename it to [\support] then it will be fixed.

 

Cheers,

Pete Houston

 

Categories: Tricks & Tips Tags: ,

Android SDK v4.1 – Jelly Bean released! Check it out


 

Yeah, it’s like Google has decided to release the SDK sooner than mid-July…anyway, only Nexus Q to test 😦

Android v4.1 SDK Update

Android v4.1 SDK Update

Categories: Of Diary Tags: , ,