Android's useful code snippets are common code summaries
- 2020-06-03 08:27:08
- OfStack
1: Check whether the memory card is inserted
String status=Environment.getExternalStorageState();
if(status.equals(Enviroment.MEDIA_MOUNTED))
{
That there are SD Card into
}
2: Make an Activity transparent
Layout ES11en. setTheme(R. style. Theme_Transparent) is not set in OnCreate.
Here is the definition of Theme_Transparent (note that transparent_bg is a transparent image)
3: Set the handle in the screen element
Use Activity.findViewById to get a handle on an element on the screen. Use this handle to set or get any exposed value for the object.
TextView msgTextView = (TextView)findViewById(R.id.msg);
msgTextView.setText(R.string.push_me);
4: Send text messages
String body="this is mms demo";
Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null));
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
startActivity(mmsintent);
5: Send MMS
StringBuilder sb = new StringBuilder();
sb.append("file://");
sb.append(fd.getAbsoluteFile());
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mmsto", number, null));
// Below extra datas are all optional.
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);
startActivity(intent)
6: Send Mail
mime = "img/jpg";
shareIntent.setDataAndType(Uri.fromFile(fd), mime);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fd));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(Intent.EXTRA_TEXT, body);
7: Register 1 BroadcastReceiver
registerReceiver(mMasterResetReciever, new IntentFilter("oms.action.MASTERRESET"));
private BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if("oms.action.MASTERRESET".equals(action)){
RecoverDefaultConfig();
}
}
}
8: Define ContentObserver to listen to a table
private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI);
private class DownloadsChangeObserver extends ContentObserver {
public DownloadsChangeObserver(Uri uri) {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) { }
}
9: Get the UA
public String getUserAgent()
{
String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
return user_agent;
}
10: Empty your phone of Cookie
CookieSyncManager.createInstance(getApplicationContext());
CookieManager.getInstance().removeAllCookie();11 : to establish a GPRS The connection
//Dial the GPRS link.
private boolean openDataConnection() {
// Set up data connection.
DataConnection conn = DataConnection.getInstance();
if (connectMode == 0) {
ret = conn.openConnection(mContext, "cmwap", "cmwap", "cmwap");
} else {
ret = conn.openConnection(mContext, "cmnet", "", "");
}
}
12: PreferenceActivity
public class Setting extends PreferenceActivity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings) ;
}
}
Setting.xml:
android:key="seting2 "
android:title="@string/seting2 "
android:summary="@string/seting2 " />
android:key="seting1 "
android:title="@string/seting1 "
android:summaryOff="@string/seting1summaryOff"
android:summaryOn="@stringseting1summaryOff"/>
13: Gets data from the specified server via HttpClient
TextView msgTextView = (TextView)findViewById(R.id.msg);
msgTextView.setText(R.string.push_me);
0