This example also show different ContentResolver return with different context passed.
MainActivity.java
package com.example.androidhello;
import android.support.v7.app.ActionBarActivity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
setContentView(tv);
ContentResolver contentResolverA = getContentResolver();
//same ContentResolver returned
ContentResolver contentResolverB = AnotherClass.tryGetContentResolver(this);
//ContentResolver contentResolverB = AnotherClass.tryGetContentResolver(MainActivity.this);
//ContentResolver contentResolverB = AnotherClass.tryGetContentResolver(tv.getContext());
//ContentResolver contentResolverB = AnotherClass.tryGetContentResolver(getBaseContext());
//not same
//ContentResolver contentResolverB = AnotherClass.tryGetContentResolver(getApplicationContext());
String msg = this +"\n"
+ MainActivity.this + "\n"
+ tv.getContext() + "\n"
+ getBaseContext() + "\n"
+ getApplicationContext() + "\n\n";
if(contentResolverA == contentResolverB){
msg += "contentResolverA == contentResolverB\n"
+ contentResolverB;
}else{
msg += "contentResolverA != contentResolverB\n"
+ contentResolverA + "\n"
+ contentResolverB;
}
tv.setText(msg);
}
}
AnotherClass.java
package com.example.androidhello;
import android.content.ContentResolver;
import android.content.Context;
public class AnotherClass {
static public ContentResolver tryGetContentResolver(Context c){
ContentResolver contentResolver = c.getContentResolver();
return contentResolver;
}
}
No comments:
Post a Comment