|
API
Apndroid APIThere 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:
New APICalling 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 APICalling 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:
It is not necessary to put all parameters. Actually there are next ways:
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);
}
}
}
|
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?
@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)
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!
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..
@Lee.Wilmot in next release we will implement API through ordered broadcast.
Wonderful, thanks, look forward to it!
awesome tool! Can you please explain why it has to be in an activity?
Thanks for the service interface, but could you briefly document how to use it, or at least the relevant intent fields ?
Thanks,
Pent
Yeah,we all need to use it not in an Activity....
Guys, I am working on new simple API. Old one will be deprecated. Is anybody actually using the one based on Service?
I wanted to (still do) but couldn't find any documentation (I asked about it above)
Pent
@Lee (or anybody else), if you have any question, please ping me at martin.adamek@gmail.com
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?
I work the new api execute it and nothing happens just sends this:
help me..!
Where can I found the latest jar file that supports the start service functionality instead of needing to start it from an activity.