My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
API  

api
Updated Dec 26, 2011 by martin.adamek

Apndroid API

There are 2 ways to work with Apndroid from your code. The old way (deprecated, but still supported) is using Activity and the new way uses Service.

I am considering creating compatibility utility class that would allow to run New API against older versions of Apndroid installed. If you have ideas or suggestions, email me at martin.adamek at gmail.

Sample code demonstrating both ways is here: http://code.google.com/p/apndroid/source/browse/api-examples/

Feel free to discuss at http://groups.google.com/group/apndroid

API has two functions:

  • request the state of the switch
  • change the state of the switch

New API

Calling new API means invoking Service and listening to broadcasts for eventual responses

Request the state of the switch (New API)

To get the state of the switch you have to start Service using intent with action apndroid.intent.action.GET_STATUS without any extras. Result is returned as broadcast with action apndroid.intent.action.STATUS with one boolean extra with key apndroid.intent.extra.STATUS

startService(new Intent("apndroid.intent.action.GET_STATUS"));

registerReceiver(new BroadcastReceiver() {
	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals("apndroid.intent.action.STATUS")) {
		    boolean dataEnabled = intent.getBooleanExtra("apndroid.intent.extra.STATUS", true);
		    mToggle.setChecked(dataEnabled);
		}
	}
}, new IntentFilter("apndroid.intent.action.STATUS"));

Change the state of the switch (New API)

To change the state of the switch you need to start Service using intent with action apndroid.intent.action.CHANGE_STATUS This intent requires one boolean extra with key apndroid.intent.extra.STATUS and accepts also one optional boolean extra with key apndroid.intent.extra.KEEP_MMS_ON

Intent intent = new Intent("apndroid.intent.action.CHANGE_STATUS");
intent.putExtra("apndroid.intent.extra.STATUS", mToggle.isChecked());
intent.putExtra("apndroid.intent.extra.KEEP_MMS_ON", mMmsCheckbBox.isChecked());
startService(intent);

registerReceiver(new BroadcastReceiver() {
	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals("apndroid.intent.action.STATUS")) {
		    boolean dataEnabled = intent.getBooleanExtra("apndroid.intent.extra.STATUS", true);
		    mToggle.setChecked(dataEnabled);
		}
	}
}, new IntentFilter("apndroid.intent.action.STATUS"));

Old API

Calling old API means invoking Activity and waiting on Activity result for eventual response.

Request the state of the switch (Old API)

WARNING - Apndroid 3.0.15 in the Market contains bug that "APN_STATE" and "MMS_STATE" extras are booleans instead of integers. It will be fixed with next update!

To get the switch state, you need to start activity using intent with action com.google.code.apndroid.intent.action.STATUS_REQUEST without parameters.

As a result you should receive intent with action com.google.code.apndroid.intent.REQUEST_RESULT. This intent have a bundle with response. Bundle always contains integer value by key APN_STATE. If value == 1 than status is ON, else status is OFF. If current status is OFF then bundle contains a key with current MMS status. It's also an integer with same semantics and the key is MMS_STATE.

Code snippet for status request:

static final GET_STATE_REQUEST = 1;

startActivityForResult(new Intent("com.google.code.apndroid.intent.action.STATUS_REQUEST"), GET_STATE_REQUEST);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (requestCode == GET_STATE_REQUEST && resultCode == RESULT_OK && intent != null) {
        if (intent.getAction().equals("com.google.code.apndroid.intent.REQUEST_RESULT")) {
            boolean dataEnabled = (intent.getIntExtra("APN_STATE", 1) == 1);
            mToggle.setChecked(dataEnabled);
        }
    }
}

Change the state of the switch (Old API)

To get current apn status you need to start Activity using intent with action com.google.code.apndroid.intent.action.CHANGE_REQUEST with next parameters:

  1. com.google.code.apndroid.intent.extra.TARGET_STATE
  2. com.google.code.apndroid.intent.extra.TARGET_MMS_STATE

It is not necessary to put all parameters. Actually there are next ways:

  • put no parameters. In this case Apndroid will perform switch to another state using default preferences (user entered in Apndroid Settings) for MMS keeping
  • put com.google.code.apndroid.intent.extra.TARGET_STATE parameter only. In this case will be used default MMS keeping and switch will be performed to passed state
  • put all parameters for full control of switching. This way has a feature - if passed com.google.code.apndroid.intent.extra.TARGET_MMS_STATE differ from current keep mms setting it will be updated in app settings, so use it carefully.

If current state equals to passed target state switch will be treated as successfull.

As a result you should receive intent with action com.google.code.apndroid.intent.REQUEST_RESULT. This intent have a bundle with response. Bundle always contains boolean value by key SWITCH_SUCCESS. If value is true then switch was successfull, else unsuccessfull.

Code snippet for switch change:

static final CHANGE_STATE_REQUEST = 2;

Intent intent = new Intent("com.google.code.apndroid.intent.action.CHANGE_REQUEST");
intent.putExtra("com.google.code.apndroid.intent.extra.TARGET_STATE", mToggle.isChecked());
intent.putExtra("com.google.code.apndroid.intent.extra.TARGET_MMS_STATE", mMmsCheckbBox.isChecked());
startActivityForResult(intent, CHANGE_STATE_REQUEST);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (requestCode == GET_STATE_REQUEST && resultCode == RESULT_OK && intent != null) {
        if (intent.getAction().equals("com.google.code.apndroid.intent.REQUEST_RESULT")) {
            boolean dataEnabled = (intent.getIntExtra("APN_STATE", 1) == 1);
            mToggle.setChecked(dataEnabled);
        }
    }
}
Comment by rjgr...@gmail.com, Jan 29, 2010

I have a problem with the API. It needs to be started from an Activity since onActivityResult is called to provide the response, but I need to call it from a Service, which has a method startActivity() but no onActivityResult().

How can I do that?

Comment by project member zeldigas, Feb 1, 2010

@rjgruet In current API implementation it can be impossible. Frankly speeking i don't know is this technically possible. Later we can try to add some additional API for services needs (or you can send us a patch if you want this feature support faster)

Comment by Lee.Wil...@gmail.com, Mar 23, 2010

Hello, I'm writing an automation app called Tasker http://tasker.dinglisch.net. I need access to the current state (enabled/disabled) from a service. Could you perhaps accept a status inquiry via e.g. an ordered broadcast (sendOrderedBroadcast) rather than just via an activity startup ?

Thanks!

Comment by sere...@gmail.com, Mar 26, 2010

If I send CHANGE_REQUEST intent with only TARGET_STATE specified APNDroid will still post notification, nevertheless I set it off in settings. Reading this "put TARGET_APN_STATE parameeter only. In this case will be used default mms keeping and notification settings and switch will be performed to passed state." I was assuming that it should use app settings for notification..

Comment by project member zeldigas, Apr 20, 2010

@Lee.Wilmot in next release we will implement API through ordered broadcast.

Comment by Lee.Wil...@gmail.com, Apr 29, 2010

Wonderful, thanks, look forward to it!

Comment by guyhagem...@gmail.com, Sep 15, 2010

awesome tool! Can you please explain why it has to be in an activity?

Comment by Lee.Wil...@gmail.com, Oct 9, 2010

Thanks for the service interface, but could you briefly document how to use it, or at least the relevant intent fields ?

Thanks,

Pent

Comment by feng....@gmail.com, Sep 13, 2011

Yeah,we all need to use it not in an Activity....

Comment by project member martin.adamek, Nov 29, 2011

Guys, I am working on new simple API. Old one will be deprecated. Is anybody actually using the one based on Service?

Comment by Lee.Wil...@gmail.com, Dec 26, 2011

I wanted to (still do) but couldn't find any documentation (I asked about it above)

Pent

Comment by project member martin.adamek, Dec 26, 2011

@Lee (or anybody else), if you have any question, please ping me at martin.adamek@gmail.com

Comment by gmu...@gmail.com, Jan 3, 2012

Hello, I am new to android and wanted to make an app that could turn of 3G on or off on a click of a button i looked around a bit and found this which looked like the perfect thing but it does nothing when i run it. I used the files (in the examples part of the project) provided to build my app but it seems to have no effect can someone please help?

Comment by netok...@gmail.com, Jan 5, 2012

I work the new api execute it and nothing happens just sends this:

Unable to start service Intent {act = apndroid.intent.action.CHANGE_STATUS}: not found

help me..!

Comment by jfma...@gmail.com, Feb 19, 2012

Where can I found the latest jar file that supports the start service functionality instead of needing to start it from an activity.


Sign in to add a comment
Powered by Google Project Hosting