Is it possible to retrieve the time an account was last synchronized, like the system Settings->Accounts&Sync app does? I'm using Android 2.2.
Looking at the 2.2 source for AccountSyncSettings.java, I see the status is retrieved using:
SyncStatusInfo status = ContentResolver.getSyncStatus(account, authority);
but SyncStatusInfo and getSyncStatus don't seem to be part of the public API (marked with #hide). Is there some other way to get at this info?
You can use reflection to achieve this purpose.Here is my code to implement this
private long getLasySyncTime() {
long result = 0;
try {
Method getSyncStatus = ContentResolver.class.getMethod(
"getSyncStatus", Account.class, String.class);
if (mAccount != null && mSyncAdapter != null) {
Object status = getSyncStatus.invoke(null, mAccount,
mSyncAdapter.authority);
Class<?> statusClass = Class
.forName("android.content.SyncStatusInfo");
boolean isStatusObject = statusClass.isInstance(status);
if (isStatusObject) {
Field successTime = statusClass.getField("lastSuccessTime");
result = successTime.getLong(status);
TLog.d(WeixinSetting.class, "get last sync time %d", result);
}
}
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
TLog.d(WeixinSetting.class, e.getMessage() + e.getCause().getMessage());
} catch (IllegalArgumentException e) {
} catch (ClassNotFoundException e) {
} catch (NoSuchFieldException e) {
} catch (NullPointerException e) {
}
return result;
}
The Settings app uses ContentResolver.getSyncStatus(account, authority). However, this is not part of the public API. You can use it, but it could break with any future release.
Related
I used this code it's working when i called from activity and fragment
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
Info adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesAvailabilityException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
String AdId = adInfo.getId();
But when i called from pending intent like Package Removed then i want to call the web service at that time i need advertising id but i got null.if you people had done previously please suggest me.thanks in advance.
try use app context
as receivers could use activity context or app context - depends who&how started the receiver
AdvertisingIdClient.getAdvertisingIdInfo(context.getApplicationContext());
You can try this code and call the bellow method on onCreate
public void getAAID()
{
AsyncTask.execute(new Runnable() {
#Override
public void run() {
try {
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(MyActivity.this);
String myId = adInfo != null ? adInfo.getId() : null;
Log.i("UIDMY",myId);
} catch (Exception e) {
Log.e("error", e);
}
}
});
}
Check complete post: how to get AAID programmatically
I've been struggling around with this test. I want to test my created ContentProvider, using RoboGuice.
Currently the ContentProvider I have extends from RoboContentProvider.
However, I'm not being able to Make It Run.
It says there's no implementation for an Interface. However, the modules are being defined and so one.
Here's the code for the setUp Method on the AndroidTestCase.
#Override
public void setUp() {
Context context = InstrumentationRegistry.getTargetContext();
Application app = null;
try {
app = InstrumentationRegistry
.getInstrumentation()
.newApplication(Application.class.getClassLoader(), Application.class.getName(), context);
RoboGuice.setUseAnnotationDatabases(false);
RoboGuice.overrideApplicationInjector(app,
RoboGuice.newDefaultRoboModule(app),
new DatabaseModule(app)
);
mContentResolver = new MockContentResolver();
mContentProvider = new MobiModelsContentProvider();
mContentProvider.attachInfo(app, null);
mContentResolver.addProvider("com.authority.models.test", mContentProvider);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
I developing VOIP android application that make and receive the sip call.I Build the pjsip lilbrary as described in "http://trac.pjsip.org/repos/wiki/Getting-Started/Android".
1. Hold
MainActivity.prm.setOptions(pjsua_call_flag.PJSUA_CALL_UPDATE_CONTACT
.swigValue());
try {
MainActivity.currentCall.setHold(MainActivity.prm);
} catch (Exception e) {
e.printStackTrace();
}
I found this code on pjsip documentation,but this code does not work for put a call on Hold.There is no error message return.
2.Unhold
MainActivity.prm = new CallOpParam(true);
MainActivity.prm.getOpt().setFlag(1);
try {
MainActivity.currentCall.reinvite(MainActivity.prm);
} catch (Exception e) {
e.printStackTrace();
}
Thanks.
Here is my code for hold and unHold:
public void setHold(boolean hold) {
if ((localHold && hold) || (!localHold && !hold)) return;
if(currentCall == null) return;
CallOpParam param = new CallOpParam(true);
try {
if (hold) {
currentCall.setHold(param);
localHold = true;
} else {
CallSetting opt = param.getOpt();
opt.setAudioCount(1);
opt.setVideoCount(0);
opt.setFlag(pjsua_call_flag.PJSUA_CALL_UNHOLD.swigValue());
currentCall.reinvite(param);
localHold = false;
}
} catch (Exception e) {}
}
I hope it's helpful.
I'm using pjsua2 with Android build version 2.2.1.
I can put a call on hold using:
CallOpParam prm = new CallOpParam();
prm.setOptions(pjsua_call_flag.PJSUA_CALL_UPDATE_CONTACT.swigValue());
try {
currentCall.setHold(prm)
} catch(Exception e) {
e.printStackTrace();
}
To unhold call I tried this, but does not work:
CallOpParam prm = new CallOpParam();
prm.setOptions(pjsua_call_flag.PJSUA_CALL_UNHOLD.swigValue());
try {
currentCall.reinvite(prm);
} catch(Exception e) {
e.printStackTrace();
}
Is that a bug of pjsua? How should I call the reinvite method?
Look my code:
public void holdCall() {
CallOpParam prm = new CallOpParam(true);
try {
currentCall.setHold(prm);
} catch (Exception e) {
e.printStackTrace();
}
}
public void unHoldCall() {
CallOpParam prm = new CallOpParam(true);
prm.getOpt().setFlag(1);
try {
currentCall.reinvite(prm);
} catch (Exception e) {
e.printStackTrace();
}
}
According to this issue, it's necessary to set flag on CallOpParam.
The constant PJSUA_CALL_UNHOLD == 1, but I couldn't use PJSUA_CALL_UNHOLD directly.
I'm using Asterisk and it's working.
To unhold the call I need this in version 2.4.5:
CallOpParam prm = new CallOpParam();
CallSetting opt = prm.getOpt();
opt.setAudioCount(1);
opt.setVideoCount(0);
opt.setFlag(pjsua_call_flag.PJSUA_CALL_UNHOLD.swigValue());
call.reinvite(prm);
Here is another example:
public void setHold(boolean hold) {
CallOpParam param = new CallOpParam();
try {
if (hold) {
setHold(param);
} else {
CallSetting opt = param.getOpt();
opt.setAudioCount(1);
opt.setVideoCount(0);
opt.setFlag(pjsua_call_flag.PJSUA_CALL_UNHOLD.swigValue());
reinvite(param);
}
} catch (Exception exc) {
String operation = hold ? "hold" : "unhold";
Logger.error(LOG_TAG, "Error : ", exc);
}
}
You can find here the full implementation.
I want to be able to turn off overscroll (the glowing effect when reaching the top or bottom of a page in 2.3+) however I also want my code to run in older versions of android that don't even have overscroll functionality. As per the documentation here: Android Backwards Compatibility I am using reflection in my custom webview class to call setOverScrollMode however everytime I call this on a device running 2.3.4, I get a NoSuchMethodException. Any idea why I can't retrieve this method?
Strangely, if I just call setOverScrollMode without any reflection, it works, so the method is definitely there.
public class MyWebView extends WebView{
public void compatibilitySetOverScroll(){
try {
Method mWebview_SetOverScroll = WebView.class.getMethod("setOverScrollMode", new Class[] { Integer.class } );
/* success, this is a 2.3+ */
if (mWebview_SetOverScroll != null) {
try {
mWebview_SetOverScroll.invoke(this, 2);
} catch (InvocationTargetException ite) {
throw new RuntimeException(ite.getCause());
} catch (IllegalAccessException ie) {
System.err.println("unexpected " + ie);
}
}
} catch (NoSuchMethodException nsme) {
/* failure, must be older device */
}
}
}
Try Integer.TYPE instead of Integer.class
More correct version:
public static void disableOverscroll(View view) {
Class<?> viewCls = view.getClass();
try {
Method m = viewCls.getMethod("setOverScrollMode",
new Class[] { int.class });
int OVER_SCROLL_NEVER = (Integer) viewCls.getField(
"OVER_SCROLL_NEVER").get(view);
m.invoke(view, OVER_SCROLL_NEVER);
} catch (Exception e) {
// swallow
}
}
another way :
try
{
Class<?> myTarget = Class.forName("android.widget.HorizontalScrollView");
Method myMethod = myTarget.getDeclaredMethod("setOverScrollMode", Integer.TYPE);
myMethod.invoke(scrollView, 2);
}
catch (Exception e)
{
e.printStackTrace();
}