Launch your applications on custom secret code

August 13, 2012 8 comments

You might know about some of Android Secret Number or Code, right?

For sample, on the dial pad, type this code: *#*#4636#*#*, the a Testing screen will appear; it’s a secret screen that you do/don’t know about it before.

Dial Pad on SecretCode

Dial Pad on SecretCode

The interesting point here is that you absolutely want to do the same thing, type your custom secret code and your app is launched immediately.

This is what you need to do for it.

1. Create a custom BroadcastReceiver for Secret Code entering; for sample, I named it MySecretCodeReceiver.

This receiver will launch my Activity on detection.

public class MySecretCodeReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
			Intent i = new Intent(context, MainActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
		}
	}

}

2. Register the MySecretCodeReceiver properly, like below:

        <receiver android:name=".MySecretCodeReceiver">
    	 	<intent-filter>
          		<action android:name="android.provider.Telephony.SECRET_CODE" />
         		<data android:scheme="android_secret_code" android:host="1711" />
   		</intent-filter>
        </receiver>

You need to make sure that the tag <action> should be like exactly same as above, since it’s the pre-defined action for SECRET_CODE handling. Also, pick a number in attribute “android:host” to your application. For sample here, I pick my lucky number 1711.

That’s pretty much straightforward. Have fun anyway!

Cheers,

Pete Houston

Advertisements
Categories: Tricks & Tips Tags: , , ,

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

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: ,