MMS发送流程(代码版)android

MMS发送流程(代码版)android
MMS发送流程(代码版)android

MMS发送流程(代码版)

Android2.2packages/apps/Mms

1. 点击发送按钮Src/com/android/mms/ui/ComposeMessageActivity.java

public void onClick(View v) {

if ((v == mSendButton) && isPreparedForSending()) {

confirmSendMessageIfNeeded(); //确认是否需要发送短信—-》

}

}

2.src/com/android/mms/ui/ComposeMessageActivity.java

private void confirmSendMessageIfNeeded() {

if (!isRecipientsEditorVisible()) { //编辑联系人不可见时,也就是给已存在会话的联系人发送短信时

sendMessage(true);

return;

}

boolean isMms = mWorkingMessage.requiresMms(); //是否需要以彩信形式发送

if (mRecipientsEditor.hasInvalidRecipient(isMms)) {//是否含有不合法的收件人

if (mRecipientsEditor.hasValidRecipient(isMms)) {//有合法的和不合法的,弹出尝试发送对话框

String title = getResourcesString(R.string.has_invalid_recipient,

mRecipientsEditor.formatInvalidNumbers(isMms));

new AlertDialog.Builder(this)

.setIcon(android.R.drawable.ic_dialog_alert)

.setTitle(title)

.setMessage(R.string.invalid_recipient_message)

.setPositiveButton(R.string.try_to_send,

newSendIgnoreInvalidRecipientListener())

.setNegativeButton(R.string.no, new CancelSendingListener())

.show();

} else {//如果全是不合法的联系人,提示不能发送信息

new AlertDialog.Builder(this)

.setIcon(android.R.drawable.ic_dialog_alert)

.setTitle(R.string.cannot_send_message)

.setMessage(R.string.cannot_send_message_reason)

.setPositiveButton(R.string.yes, new CancelSendingListener())

.show();

}

} else {//判断收件人没有问题,接着发送信息 --》

sendMessage(true);

}

}

3. src/com/android/mms/ui/ComposeMessageActivity.java

private void sendMessage(boolean bCheckEcmMode) {

Log.v(TAG, "sendMessage");

if (bCheckEcmMode) {

// TODO: expose this in telephony layer for SDK build

String inEcm =

SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE); //判断电话是否处于紧急拨号模式,得到的inEcm一般为空

Log.v(TAG, "inEcm = " + inEcm);

if (Boolean.parseBoolean(inEcm)) {

try {

startActivityForResult(

new

Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS,null),

REQUEST_CODE_ECM_EXIT_DIALOG);

return;

} catch (ActivityNotFoundException e) {

// continue to send message

Log.e(TAG, "Cannot find EmergencyCallbackModeExitDialog", e);

}

}

}

if (!mSendingMessage) {

// send can change the recipients. Make sure we remove the listeners firstand then add

// them back once the recipient list has settled.

removeRecipientsListeners(); //取消对收件人的监听

mWorkingMessage.send(); //发送信息—-》

mSentMessage = true;

mSendingMessage = true;

addRecipientsListeners(); //重新添加收件人监听

}

// But bail out if we are supposed to exit after the message is sent.

if (mExitOnSent) {//如果mExitOnSent为true,信息发送完成后退出Activity

finish();

}

}

4. src/com/android/mms/data/WorkingMessage.java

/**

* Send this message over the network. Will call back with onMessageSent() once

* it has been dispatched to the telephonystack. This WorkingMessage object is

* no longer useful after this method hasbeen called.

*/

public void send() {

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

LogTag.debug("send");

}

// Get ready to write to disk.

prepareForSave(true /* notify */);//主要做一下同步收件人和WorkingMessage,彩信时在准备其他一些东西

// We need the recipient list for both SMS and MMS.

final Conversation conv = mConversation;

String msgTxt = mText.toString();

Log.v(TAG, "msgText = " + msgTxt);

if (requiresMms()|| addressContainsEmailToMms(conv, msgTxt)) {

// Make local copies of the bits we need for sending a message,

// because we will be doing it off of the main thread, which will

// immediately continue on to resetting some of this state.

final Uri mmsUri = mMessageUri; //如果第一次发送,此时mmsUri为null,如果是重发,则是草稿箱的地址 mMessageUri =content://mms/drafts/1

final PduPersister persister = PduPersister.getPduPersister(mContext);

final SlideshowModel slideshow = mSlideshow;

final SendReq sendReq = makeSendReq(conv,mSubject);

// Do the dirty work of sending the message off of the main UI thread.

new Thread(new Runnable() {

public void run() {

// Make sure the text in slide 0 is no longer holding onto a reference to

// the text in the message text box.

slideshow.prepareForSend();

sendMmsWorker(conv, mmsUri, persister, slideshow, sendReq);

}

}).start();

}else {

// Same rules apply as above.

final String msgText = mText.toString();//取出短消息

Log.v(TAG, "msgText = " + msgText);

new Thread(new Runnable() {

public void run() {

preSendSmsWorker(conv, msgText);//发送信息--》

}

}).start();

}

// update the Recipient cache with the new to address, if it's different

RecipientIdCache.updateNumbers(conv.getThreadId(),conv.getRecipients());

// Mark the message as discarded because it is "off the market"after being sent.

mDiscarded = true;

}

5. src/com/android/mms/data/WorkingMessage.java

private void sendMmsWorker(Conversation conv, Uri mmsUri, PduPersisterpersister, SlideshowModel slideshow, SendReq sendReq) {

Log.v(TAG, "sendMmsWorker");

// If user tries to send the message, it's a signal the inputtedtext is what they wanted.

https://www.360docs.net/doc/3315511464.html,erAcceptedImeText(mContext);

// First make sure we don't have too many outstanding unsent message.

Cursor cursor = null;

try {

cursor = SqliteWrapper.query(mContext, mContentResolver,

Mms.Outbox.CONTENT_URI,MMS_OUTBOX_PROJECTION, null, null, null);

if (cursor != null) {//如果MMS_OUTBOX里有未发送的彩信,并且总的大小已经超过了彩信的最大限制,则取消此次发送,并存入草稿箱

Log.v(TAG, "query Mms.Outbox.CONTENT_URI is not empty");

long maxMessageSize = MmsConfig.getMaxSizeScaleForPendingMmsAllowed()*

MmsConfig.getMaxMessageSize();

Log.v(TAG, "MmsConfig.getMaxSizeScaleForPendingMmsAllowed() =" + MmsConfig.getMaxSizeScaleForPendingMmsAllowed());

Log.v(TAG, "MmsConfig.getMaxMessageSize()() = " +

MmsConfig.getMaxMessageSize());

long totalPendingSize = 0;

while (cursor.moveToNext()) {

totalPendingSize +=cursor.getLong(MMS_MESSAGE_SIZE_INDEX);

Log.v(TAG, "totalPendingSize = " + totalPendingSize);

}

if (totalPendingSize >= maxMessageSize) {

unDiscard(); // itwasn't successfully sent. Allow it to be saved as a draft.

mStatusListener.onMaxPendingMessagesReached();

return;

}

}else{

Log.v(TAG, "query Mms.Outbox.CONTENT_URI is empty");

}

} finally {

if (cursor != null) {

cursor.close();

}

}

mStatusListener.onPreMessageSent();

// Make sure we are still using the correct thread ID for our

// recipient set.

long threadId = conv.ensureThreadId();

if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {

LogTag.debug("sendMmsWorker: update draft MMS message " + mmsUri); }

if (mmsUri == null) {//如果是首次发送,先把彩信保存入草稿箱// Create a new MMS message if one hasn't been made yet.

Log.v(TAG, "mmsUri == null and startcreateDraftMmsMessage");

mmsUri = createDraftMmsMessage(persister,sendReq, slideshow);

} else {

// Otherwise, sync the MMS message in progress to disk.

Log.v(TAG, "mmsUri = " + mmsUri);

Log.v(TAG, "updateDraftMmsMessage");

updateDraftMmsMessage(mmsUri,persister, slideshow, sendReq); }

// Be paranoid and clean any draft SMS up. deleteDraftSmsMessage(threadId);

// Resize all the resizeable attachments (e.g. pictures) to fit // in the remaining space in the slideshow.

int error = 0;

try {

slideshow.finalResize(mmsUri);

} catch (ExceedMessageSizeException e1) {

error = MESSAGE_SIZE_EXCEEDED;

} catch (MmsException e1) {

error = UNKNOWN_ERROR;

}

if (error != 0) {

markMmsMessageWithError(mmsUri);

mStatusListener.onAttachmentError(error);

return;

}

MessageSender sender = new MmsMessageSender(mContext, mmsUri, slideshow.getCurrentMessageSize());

try {

if (!sender.sendMessage(threadId)) {

// The message was sent through SMS protocol, we should

// delete the copy which was previously saved in MMS drafts.

SqliteWrapper.delete(mContext, mContentResolver, mmsUri, null, null);

}

// Make sure this thread isn't over the limits in message count

Recycler.getMmsRecycler().deleteOldMessagesByThreadId(mContext, threadId);

} catch (Exception e) {

Log.e(TAG, "Failed to send message: " + mmsUri + ",threadId=" + threadId, e);

}

mStatusListener.onMessageSent();

}

6.src/com/android/mms/transaction/MmsMessageSender.java

public boolean sendMessage(long token) throws MmsException {

// Load the MMS from the message uri

PduPersister p = PduPersister.getPduPersister(mContext);

GenericPdu pdu = p.load(mMessageUri);

if (pdu.getMessageType() != PduHeaders.MESSAGE_TYPE_SEND_REQ){

throw new MmsException("Invalid message: " +pdu.getMessageType());

}

SendReq sendReq = (SendReq)pdu;

// Update headers.

updatePreferencesHeaders(sendReq);

// MessageClass.

sendReq.setMessageClass(DEFAULT_MESSAGE_CLASS.getBytes());

// Update the 'date' field of the message before sending it.

sendReq.setDate(System.currentTimeMillis()/ 1000L);

sendReq.setMessageSize(mMessageSize);

p.updateHeaders(mMessageUri, sendReq);

// Move the message into MMS Outbox

p.move(mMessageUri, Mms.Outbox.CONTENT_URI);

// Start MMS transaction service

SendingProgressTokenManager.put(ContentUris.parseId(mMessageUri), token);

mContext.startService(new Intent(mContext, TransactionService.class));

return true;

}

7.src/com/android/mms/transaction/TransactionService.java

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.v(TAG, "onStartCommand");

if (intent == null) {

return Service.START_NOT_STICKY;

}

mConnMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

boolean noNetwork =!isNetworkAvailable();

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "onStart: #" + startId + ": " + intent.getExtras() + " intent=" + intent);

Log.v(TAG, " networkAvailable=" + !noNetwork);

}

Log.v(TAG, "getAction is " + intent.getAction());

if (ACTION_ONALARM.equals(intent.getAction())|| (intent.getExtras() == null)) {

Log.v(TAG, "ACTION_ONALARM.equals(intent.getAction()) ||(intent.getExtras() == null)");

// Scan database to find all pending operations.

Cursor cursor = PduPersister.getPduPersister(this).getPendingMessages(

System.currentTimeMillis());

if (cursor != null) {

try {

int count = cursor.getCount();

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "onStart: cursor.count=" + count);

}

if (count == 0) {

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "onStart: no pending messages. Stoppingservice.");

}

RetryScheduler.setRetryAlarm(this);

stopSelfIfIdle(startId);

return Service.START_NOT_STICKY;

}

int columnIndexOfMsgId

=cursor.getColumnIndexOrThrow(PendingMessages.MSG_ID);

int columnIndexOfMsgType =cursor.getColumnIndexOrThrow(

PendingMessages.MSG_TYPE);

if (noNetwork) {

// Make sure we register for connection state changes.

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "onStart: registerForConnectionStateChanges");

}

MmsSystemEventReceiver.registerForConnectionStateChanges(

getApplicationContext());

}

while (cursor.moveToNext()) {

int msgType =cursor.getInt(columnIndexOfMsgType);

int transactionType =getTransactionType(msgType);

Log.v(TAG, "msgType = " + msgType);

Log.v(TAG, "transactionType = " + transactionType);

if (noNetwork) {

onNetworkUnavailable(startId, transactionType);

return Service.START_NOT_STICKY;

}

switch (transactionType){

case -1:

break;

case Transaction.RETRIEVE_TRANSACTION:

// If it's a transiently failed transaction,

// we should retry it in spite of current

// downloading mode.

int failureType =cursor.getInt(

cursor.getColumnIndexOrThrow(

PendingMessages.ERROR_TYPE));

if (!isTransientFailure(failureType)){

break;

}

// fall-through

default:

Uri uri =ContentUris.withAppendedId(

Mms.CONTENT_URI,

cursor.getLong(columnIndexOfMsgId));

TransactionBundle args = new TransactionBundle(

transactionType, uri.toString());

// FIXME: We use the same startId for all MMs.

launchTransaction(startId, args, false);

break;

}

}

} finally {

cursor.close();

}

} else {

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "onStart: no pending messages. Stoppingservice.");

}

RetryScheduler.setRetryAlarm(this);

stopSelfIfIdle(startId);

}

} else {

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "onStart: launch transaction...");

}

// For launching NotificationTransaction and test purpose.

TransactionBundle args = newTransactionBundle(intent.getExtras());

launchTransaction(startId, args,noNetwork);

}

return Service.START_NOT_STICKY;

}

8. src/com/android/mms/transaction/TransactionService.java

private void launchTransaction(int serviceId,TransactionBundle txnBundle, boolean noNetwork) {

Log.v(TAG, "launchTransaction");

if (noNetwork) {

Log.w(TAG, "launchTransaction: no network error!");

onNetworkUnavailable(serviceId,txnBundle.getTransactionType());

return;

}

Message msg = mServiceHandler.obtainMessage(EVENT_TRANSACTION_REQUEST);

msg.arg1 = serviceId;

msg.obj = txnBundle;

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "launchTransaction: sending message " + msg);

}

mServiceHandler.sendMessage(msg);

}

9. src/com/android/mms/transaction/TransactionService.java

private final class ServiceHandler extends Handler {

public ServiceHandler(Looper looper) {

super(looper);

}

/**

* Handle incoming transactionrequests.

* The incoming requests are initiatedby the MMSC Server or by the

* MMS Client itself.

*/

@Override

public void handleMessage(Messagemsg) {

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "Handling incoming message: " + msg);

}

Transaction transaction = null;

switch (msg.what) {

case EVENT_QUIT:

getLooper().quit();

return;

case EVENT_CONTINUE_MMS_CONNECTIVITY:

synchronized (mProcessing) {

if (mProcessing.isEmpty()) {

return;

}

}

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "handle EVENT_CONTINUE_MMS_CONNECTIVITYevent...");

}

try {

int result =beginMmsConnectivity();

if (result != Phone.APN_ALREADY_ACTIVE){

Log.v(TAG, "Extending MMS connectivity returned " + result +

" instead of APN_ALREADY_ACTIVE");

// Just wait for connectivity startup without

// any newrequest of APN switch.

return;

}

} catch (IOException e) {

Log.w(TAG, "Attempt to extend use of MMS connectivityfailed");

return;

}

// Restart timer

sendMessageDelayed(obtainMessage(EVENT_CONTINUE_MMS_CONNECTIVITY),

APN_EXTENSION_WAIT);

return;

case EVENT_DATA_STATE_CHANGED:

/*

* If we are being informedthat connectivity has been established * to allow MMS traffic,then proceed with processing the pending * transaction, if any.

*/

if (mConnectivityListener == null) {

return;

}

NetworkInfo info = mConnectivityListener.getNetworkInfo();

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "Handle DATA_STATE_CHANGED event: " + info);

}

// Check availability of the mobile network.

if ((info == null) || (info.getType() !=

ConnectivityManager.TYPE_MOBILE_MMS)) {

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, " type isnot TYPE_MOBILE_MMS, bail");

}

return;

}

if (!info.isConnected()) {

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, " TYPE_MOBILE_MMS not connected, bail");

}

return;

}

TransactionSettings settings = newTransactionSettings(

TransactionService.this,info.getExtraInfo());

// If this APN doesn't have an MMSC, wait for one that does.

if (TextUtils.isEmpty(settings.getMmscUrl())){

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, " empty MMSCurl, bail");

}

return;

}

// Set a timer to keep renewing our "lease" on the MMSconnection sendMessageDelayed(obtainMessage(EVENT_CONTINUE_MMS_CONNECTIVITY),

APN_EXTENSION_WAIT);

processPendingTransaction(transaction, settings);

return;

case EVENT_TRANSACTION_REQUEST://响应请求

Log.v(TAG, "EVENT_TRANSACTION_REQUEST");

int serviceId = msg.arg1;

try {

TransactionBundle args= (TransactionBundle) msg.obj;

TransactionSettingstransactionSettings;

// Set the connection settings for this transaction.

// If these have not been set in args, load thedefault settings.

String mmsc =args.getMmscUrl();

if (mmsc != null) {

transactionSettings= new TransactionSettings(

mmsc,args.getProxyAddress(), args.getProxyPort());

} else {

transactionSettings= new TransactionSettings(

TransactionService.this, null);

}

int transactionType =args.getTransactionType();

Log.v(TAG, "transactionType = " + transactionType);

if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {

Log.v(TAG, "handle

EVENT_TRANSACTION_REQUEST:transactionType=" +

transactionType);

}

// Create appropriate transaction

switch (transactionType){

case Transaction.NOTIFICATION_TRANSACTION:

String uri =args.getUri();

if (uri != null) {

transaction= new NotificationTransaction(

TransactionService.this, serviceId,

transactionSettings, uri);

} else {

// Now it's only used for test purpose.

byte[] pushData =args.getPushData();

PduParserparser = new PduParser(pushData);

GenericPdu ind= parser.parse();

int type =

PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND;

if ((ind != null) &&(ind.getMessageType() == type)) {

transaction = newNotificationTransaction(

TransactionService.this, serviceId,

transactionSettings, (NotificationInd) ind);

} else {

Log.e(TAG, "Invalid PUSH data.");

transaction = null;

return;

}

}

break;

case Transaction.RETRIEVE_TRANSACTION:

transaction = newRetrieveTransaction(

TransactionService.this, serviceId,

transactionSettings, args.getUri());

break;

case Transaction.SEND_TRANSACTION://根据transactiontype响

应发送彩信

android studio 控件常用属性

android studio 控件常用属性 下面是RelativeLayout各个属性 1.android:layout_above="@id/xxx" --将控件置于给定ID控件之上 2.android:layout_below="@id/xxx" --将控件置于给定ID控件之下 3. android:layout_toLeftOf="@id/xxx" --将控件的右边缘和给定ID控件的左边缘对齐 4.android:layout_toRightOf="@id/xxx" --将控件的左边缘和给定ID控件的右边缘对齐 5. android:layout_alignLeft="@id/xxx" --将控件的左边缘和给定ID控件的左边缘对齐 6.android:layout_alignTop="@id/xxx" --将控件的上边缘和给定ID控件的上边缘对齐 7.android:layout_alignRight="@id/xxx" --将控件的右边缘和给定ID控件的右边缘对齐 8.android:layout_alignBottom="@id/xxx" --将控件的底边缘和给定ID控件的底边缘对齐 9.android:layout_alignParentLeft="true" --将控件的左边缘和父控件的左边缘对齐 10. android:layout_alignParentTop="true" --将控件的上边缘和父控件的上边缘对齐 11. android:layout_alignParentRight="true" --将控件的右边缘和父控件的右边缘对齐 12.android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐 13.android:layout_centerInParent="true" --将控件置于父控件的中心位置 14.android:layout_centerHorizontal="true" --将控件置于水平方向的中心位置 15.android:layout_centerVertical="true" --将控件置于垂直方向的中心位置 android:layout_width 设置组件的宽度 android:layout_height 设置组件的高度 android:id 给组件定义一个id值,供后期使用 android:background 设置组件的背景颜色或背景图片 android:text 设置组件的显示文字 android:textColor 设置组件的显示文字的颜色 android:layout_below 组件在参考组件的下面 android:alignTop 同指定组件的顶平行

Android界面开发之常用系统控件界面大合集

今天我用自己写的一个Demo 和大家详细介绍一个Android开发中遇到的一些常用系统控件的使用技巧。 1.文本框TextView TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView,第一种是通过xml布局文件

呈现,第二种是通过代码来呈现,由此可见Android 的界面开发真的是非常灵活。 view plaincopy to clipboardprint? 1. public class TextViewActivity extends Activity { 2. @Override 3. protected void onCreate(Bundle savedInstanceState) { 4. setContentView(https://www.360docs.net/doc/3315511464.html,yout.textview);

5. 6. LinearLayout ll = (LinearLayout) findViewById(R.id.textviewll); 7. TextView textView = new TextView(this); 8. //设置显示文字 9. textView.setText("从代码中添加一个TextView"); 10. //设置显示颜色 11. textView.setTextColor(Color.WHITE); 12. //设置显示字体大小 13. textView.setTextSize(18); 14. //设置显示背景颜色 15. textView.setBackgroundColor(Color.BLUE); 16. //设置锚点位置 17. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CE NTER_HORIZONTAL); 18. //把这个view加入到布局当中 19. ll.addView(textView); 20. 21. super.onCreate(savedInstanceState); 22. } 23. } 1. 2.

Android开发日志—常用控件篇(android简介和容器控件)

Android开发日志—常用控件篇(容器控件) android简介: Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。Android操作系统最初由Andy Rubin开发,主要支持手机。 Android的系统架构和其操作系统一样,采用了分层的架构。 Android结构 从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。开发人员也可以完全访问核心应用程序所使用的API框架。该应用程序的架构设计简化了组件的重用;任何一个应用程序都可以发布它的功能块并且任何其它的应用程序都可以使用其所发布的功能块(不过得遵循框架的安全性)。同样,该应用程序重用机制也使用户可以方便的替换程序组件。 隐藏在每个应用后面的是一系列的服务和系统, 其中包括; 丰富而又可扩展的视图(Views),可以用来构建应用程序,它包括列表(Lists),网格(Grids),文本框(Text boxes),按钮(Buttons),甚至可嵌入的web 浏览器。 内容提供器(Content Providers)使得应用程序可以访问另一个应用程序的数据(如联系人数据库),或者共享它们自己的数据 资源管理器(Resource Manager)提供非代码资源的访问,如本地字符串,图形,和布局文件(Layout files )。 通知管理器(Notification Manager) 使得应用程序可以在状态栏中显示自定义的提示信息。 活动管理器( Activity Manager) 用来管理应用程序生命周期并提供常用的导航回退功能。 Android开发四大组件分别是:活动(Activity):用于表现功能。服务(Service):后台运行服务,不提供界面呈现。广播接收器(BroadcastReceiver):用于接收广播。内容提供商(Content Provider):支持在多个应用中存储和读取数据,相当于数据库。 活动(当前界面视图):

Android常用控件

《Android基础应用》 Android常用控件 ?本章任务 ?使用Android开发使用时间组件 ?使用Android开发使用进度条组件 ?使用Android开发创建底部选项卡 ?本章目标 ?了解Android的组件层次结构 ?掌握常用的日期时间类控件 ?掌握常用的几个容器组件 1.Android组件的层次结构 UI组件都是View的子类,View有很多子类,它们之间存在树状的继承关系View及其子类结构图

TextView及其子类结构图

ViewGroup及其子类结构图 其下的子类一般作为容器或布局来使用 FrameLayout及其子类结构图 其下的子类通常作为容器或布局来使用

2.时间控件 2.1日期时间选择器 DatePicker组件可用于输入日期,TimePicker组件可用来选择时间,只能输入小时和分,默认12小时制 DatePicker ●使用onDateChangedListener监听器来获取用户的日期选择 ●使用init对组件进行初始化 ●使用getYear,getMonth,getDayOfMonth方法获得用户选择的年,月,日 TimePicker ●使用onTimeChangedListener监听器获取用户的时间选择 ●使用setIs24HourView设置是否以24小时制显示 ●使用getCurrentHour获得当前的小时数 ●使用getCurrentMinute获得当前的分钟数 示例

示例的实现结果

2.2时钟组件 AnalogClock组件用来以表盘的方式显示当前时间,该表只有时针和分针,DigitClock组件以数字的方式显示当前时间可以显示时分秒,由于DigitClock继承TextView,可以使用TextView 的属性 示例

android常用控件大全

android常用控件大全 在Android中使用各种控件(View) DatePicker-日期选择控件 TimePicker-时间选择控件 ToggleButton-双状态按钮控件 EditText-可编辑文本控件 ProgressBar-进度条控件 SeekBar-可拖动的进度条控件 AutoCompleteTextView-支持自动完成功能的可编辑文本控件 MultiAutoCompleteTextView-支持自动完成功能的可编辑文本控件,允许输入多值(多值之间会自动地用指定的分隔符分开) ZoomControls-放大/缩小按钮控件 Include-整合控件 VideoView-视频播放控件 WebView-浏览器控件 RatingBar-评分控件 Tab-选项卡控件 Spinner-下拉框控件 Chronometer-计时器控件 ScrollView-滚动条控件 在Android中使用的Layout FrameLayout:里面只可以有一个控件,并且不能设计这个控件的位置,控件会放到左上角 LinearLayout:里面可以放多个控件,但是一行只能放一个控件

TableLayout:这个要和TableRow配合使用,很像html里面的table AbsoluteLayout:里面可以放多个控件,并且可以自己定义控件的x,y的位置 RelativeLayout:里面可以放多个控件,不过控件的位置都是相对位置 (Android界面布局好像还可以直接引用一些view,如ScrollView等) 常用控件: 1,EditText 主要函数:setText/getText设置/获取文本内容,setHint设置缺省显示内容; 2,RadioGroup,RadioButton RadioButton的isChecked()判断是否被选中 获取选中RadioButon的ID:设置 RadioGroup.setOnCheckedChangeListener方法 publiconCheckedChanged(RadioGroupgroup,intcheckedId)//checkedId 是选中RadioButton的ID 3,CheckBox isChecked()判断是否被选中 setOnCheckedChangeListener方法监视选中状态改变情况 4,Spinner a,显示数据 1),硬编码加载 通过setAdapter方法设置类型为 ArrayAdapter(Contextcontext,inttextViewResId,String[]objects) textViewResourceId:显示内容的ViewID默认设置为 https://www.360docs.net/doc/3315511464.html,yout.simple_spinner_item

实验二 android基本控件实验

实验二 android基本控件实验 【目的】 熟悉Android常用控件的基本操作,掌握它们的基本使用方法。了解控件之间的构成关系,熟悉适配器的使用原理。 【要求】 1、了解控件的继承关系; 2、掌握适配器的使用; 3、掌握信息提示的实现机制; 4、实现基本界面。 【原理】 1.控件类之间的关系 android.view.View类(视图类)呈现了最基本的UI构造块。View类是Android中的一个超类,几乎包含了所有的屏幕类型,主要负责绘制和事件处理。 Android中控件类的扩展结构如图所示。 View有众多的扩展者,它们大部分是在android.widget包中,这些继承者实际上就是Android 系统中的“控件”。View实际上就是各个控件的基类,创建交互式的图形用户界面的基础。View的直接继承者包括文本视图(TextView)、图像视图(ImageView)、进度条(ProgressBar)等。它们各自又有众多的继承者。每个控件除了继承父类功能之外,一般还具有自己的公有 方法、保护方法、XML属性等。 在Android中使用各种控件的一般情况是在布局文件中可以实现UI的外观,然后在Java文件中实现对各种控件的控制动作。控件类的名称也是它们在布局文件XML中使用的标签名称。

2.控件通用行为和属性 View是Android中所有控件类的基类,因此View中一些内容是所有控件类都具有的通用行为和属性。 提示:由于Java语言不支持多重继承,因此Android控件不可能以基本功能的“排列组合”的方式实现。在这种情况下,为了实现功能的复用,基类的功能往往做得较强,作为控件的祖先类,View所实现的功能也是最多的。 控件类经常在布局文件中使用,因此其可以使用XML属性(XMLAttributes),和Java代码经常具有对应关系。 View作为各种控件的基类,其XML属性所有控件通用,XML属性及其对应的方法如表1所示。 表1 View中的XML属性及其对应的方法 其中,android:id表示控件的标识,通常需要在布局文件中指定这个属性。View中与控件标识相关的几个方法如下所示: public int getId() // 获得控件的id(int类型)

android 控件常用属性

第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_alignParentLeft 贴紧父元素的左边缘 android:layout_alignParentRight 贴紧父元素的右边缘 android:layout_alignParentTop 贴紧父元素的上边缘 android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物 第二类:属性值必须为id的引用名“@id/id-name” android:layout_below 在某元素的下方 android:layout_above 在某元素的的上方 android:layout_toLeftOf 在某元素的左边 android:layout_toRightOf 在某元素的右边 android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐 android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐 android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐 android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐 第三类:属性值为具体的像素值,如30dip,40px android:layout_marginBottom 离某元素底边缘的距离 android:layout_marginLeft 离某元素左边缘的距离 android:layout_marginRight 离某元素右边缘的距离 android:layout_marginTop 离某元素上边缘的距离 EditText的android:hint 设置EditText为空时输入框内的提示信息。

Android控件常见的属性

一、TextView控件常见的属性

二、EditText是可编辑的文本框,继承自TextView,因此属性基本相同。EditText中的文字可以编辑而TextView只显示文字,其中的文字不能编辑。EditText和TextView类似于C#中的Label和TextBox控件。 三、Button也继承自TextView,因此也具有TextView的宽和高设置,文字显示等一些基本属性。Button一般会与单击事件联系在一起。为Button注册单击事件有两种方法: 1.通过Button控件的setOnClickListener()方法为Button 注册OnClickListener。 2.通过使用android:OnClick属性在xml布局文件中为Button 指定单击事件发生时执行的函数。

当用户点击了Button时,Android系统调用activity的selfDestruct(View view)方法。指定的这个方法必须是public的,而且只有一个View类型的参数。例如: TextView、EditText、Button都可以用setText()方法设置控件中文本的值,也都可以用getText()获取的控件的文本值。需要注意的是EditText的getText()方法返回的是Editable对象。这个对象与string对象差不多,区别是:当我们修改了一个string对象的值后会返回一个新的string对象。因为string对象不能修改,所以返回新的对象。而Editable可以修改即返回的是它本身,因此在修改的时候不必创建新的对象,节约了内存。可以用getText().toString()方法将获得的Editable对象转换为string。 四、Menu Menu 控件不需要添加在xml布局文件中,需要在Activity中复写onCreateOptionsMenu(Menu menu)方法,当我们在模拟器或者在手机

Android中常用控件List、View

Android中常用控件 ListView ListVeiw 用来展示列表的View。 使用ListView时与之相关的两个要素:适配器和数据。 适配器用来把数据映射到ListView上的中介。 数据具体的将被映射的字符串,图片,或者基本组件。 根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方便的把数据库的内容以列表的形式展示出来。 ◆适配器之ArrayAdapter private ListView listView=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(https://www.360docs.net/doc/3315511464.html,yout.main); ListView lv=(ListView)this.findViewById(R.id.listView1); lv.setAdapter(new ArrayAdapter(this,https://www.360docs.net/doc/3315511464.html,yout.simple_list_item_1,dtS ource())); } //数据源 public List dtSource(){ List list=new ArrayList(); list.add("jack"); list.add("rose"); return list; } lv.setAdapter(new ArrayAdapter(this,https://www.360docs.net/doc/3315511464.html,yout.simple_list_item_1,dtS ource()));

android开发常用控件及布局

一、开发概述 Android的控件体系分为三个类别: 1、基本控件 2、布局 3、容器 这三类控件主要分布在两个包中: 1、android.view 提供抽象接口 2、android.widget 提供系统内置的大部分控件 整个系统得控件都继承自android.view.View ,而布局及容器类继承自android.view.ViewGroup。 基本控件主要包括ImageView、ProgressBar、SurfaceView、TextView、EditText等。 布局 虽然布局跟容器均继承自android.view.ViewGroup,但并不像SWT那样提供了面板+布局 的结构,而是每种Layout就是一中特殊的面板,包括AbsoluteLayout、Fram eLayout、 LinearLayout、TableLayout、RelativeLayout等。 容器 这里的容器是指采用了特定的布局并且提供了行为的控件,包含ListView、GridView、Gallery、 Spinner等。 二、android常用控件 android常用控件之TextView、EditView、Button、Menu 示例:一个计算应用程序,输入2个数字,点击按钮进行计算两个数的积并且返回结果。 当点击模拟器右侧的Menu按钮,在输入界面中显示一个有关于和退出的菜单。 项目运行效果图: 开发步骤: 1、新建一个Android项目 2、在布局文件中依次添加EditText、TextView、EditText、Button(一定要按照界面 上控件显示的顺序在布局文件中添加) 3、再新建一个布局文件用来显示计算的结果;在这个布局文件中添加一个LinearLayout 和一个TextView(默认的布局文件中默认就有一个LinearLayout,所以不需要手动添加, 新建的布局文件默认为空,所以需要手动添加) 4、编写第一个Activity a) 让Activity继承android的Activity b) 重写Activity的onCreate()方法 c) 设置这个Activity的布局文件 d) 根据控件id获得控件对象 e) 设置控件的值,这个值引用的是string.xml中的值,所以还需要在string.xml文件中 设置(最好是使用string.xml中的值,以便实现国际化);这个在布局文件中也能设置; 5、新建一个Activity,ResultActivity a) 让Activity继承android的Activity b) 重写Activity的onCreate()方法 c) 设置这个Activity的布局文件 d) 根据控件id获得控件对象 6、在第一个Activity中编写Button的监听器

android常用系统界面控件使用大合集

常用系统控件界面大合集 今天我用自己写的一个Demo 和大家详细介绍一个Android开发中遇到的一些常用系统控件的使用技巧。 1.文本框TextView TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView,第一种是通过xml布局文件呈现,第二种是通过代码来呈现,由此可见Android 的界面非

常灵活。 1.public class TextViewActivity extends Activity { 2. @Override 3. protected void onCreate(Bundle savedInstanceState) { 4. setContentView(https://www.360docs.net/doc/3315511464.html,yout.textview); 5. 6. LinearLayout ll = (LinearLayout) findViewById(R.id.textviewll); 7. TextView textView = new TextView(this); 8. //设置显示文字 9. textView.setText("从代码中添加一个TextView"); 10. //设置显示颜色 11. textView.setTextColor(Color.WHITE); 12. //设置显示字体大小 13. textView.setTextSize(18); 14. //设置显示背景颜色 15. textView.setBackgroundColor(Color.BLUE); 16. //设置锚点位置 17. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_ HORIZONTAL);

android常用控件及布局

一、概述 Android的控件体系分为三个类别: 1、基本控件 2、布局 3、容器 这三类控件主要分布在两个包中: 1、android.view 提供抽象接口 2、android.widget 提供系统内置的大部分控件 整个系统得控件都继承自android.view.View ,而布局及容器类继承自android.view.ViewGroup。 基本控件主要包括ImageView、ProgressBar、SurfaceView、TextView、EditText等。 布局 虽然布局跟容器均继承自android.view.ViewGroup,但并不像SWT那样提供了面板+布局 的结构,而是每种Layout就是一中特殊的面板,包括AbsoluteLayout、FrameLayout、 LinearLayout、TableLayout、RelativeLayout等。 容器 这里的容器是指采用了特定的布局并且提供了行为的控件,包含ListView、GridView、Gallery、 Spinner等。 二、android常用控件 android常用控件之TextView、EditView、Button、Menu 示例:一个计算应用程序,输入2个数字,点击按钮进行计算两个数的积并且返回结果。 当点击模拟器右侧的Menu按钮,在输入界面中显示一个有关于和退出的菜单。 项目运行效果图: 开发步骤: 1、新建一个Android项目 2、在布局文件中依次添加EditText、TextView、EditText、Button(一定要按照界面 上控件显示的顺序在布局文件中添加) 3、再新建一个布局文件用来显示计算的结果;在这个布局文件中添加一个LinearLayout 和一个TextView(默认的布局文件中默认就有一个LinearLayout,所以不需要手动添加, 新建的布局文件默认为空,所以需要手动添加) 4、编写第一个Activity a) 让Activity继承android的Activity b) 重写Activity的onCreate()方法 c) 设置这个Activity的布局文件 d) 根据控件id获得控件对象 e) 设置控件的值,这个值引用的是string.xml中的值,所以还需要在string.xml文件中 设置(最好是使用string.xml中的值,以便实现国际化);这个在布局文件中也能设置; 5、新建一个Activity,ResultActivity a) 让Activity继承android的Activity b) 重写Activity的onCreate()方法 c) 设置这个Activity的布局文件 d) 根据控件id获得控件对象 6、在第一个Activity中编写Button的监听器

Android 控件布局常用属性

Android 控件布局常用属性 RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_alignParentLeft 贴紧父元素的左边缘 android:layout_alignParentRight 贴紧父元素的右边缘 android:layout_alignParentTop 贴紧父元素的上边缘 android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物 第二类:属性值必须为id的引用名“@id/id-name” android:layout_below 在某元素的下方 android:layout_above 在某元素的的上方 android:layout_toLeftOf 在某元素的左边 android:layout_toRightOf 在某元素的右边 android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐 android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐 android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐 android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐 第三类:属性值为具体的像素值,如30dip,40px android:layout_marginBottom 离某元素底边缘的距离 android:layout_marginLeft 离某元素左边缘的距离 android:layout_marginRight 离某元素右边缘的距离 android:layout_marginTop 离某元素上边缘的距离 EditText的android:hint 设置EditText为空时输入框内的提示信息。 android:gravity android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.以button为例,android:gravity="right"则button上面的文字靠右

android常用控件属性方法事件说明

1,TextView用于在界面上显示文字,内容不可编辑,继承View 2,EditText是可供用户编辑内容的文本框。继承TextView,可重用TextView定义的属性

3,按钮控件Button,继承TextView,ImageButton继承ImageView Button可根据用户的操作动态切换图片,需定义drawable资源 4,单选按钮RadioButton继承至Button按钮。可通过android:checked属性指定是否选中。 一组RadioButton只能选择其一,因此它需要与RadioGroup一起使用。 5,复选框按钮CheckBox。继承至Button按钮。可通过android:checked属性指定是否选中 7,AnalogClock继承View,重写了onDraw()方法。显示模拟时钟,不会显示当前的秒数。 DigitalClock继承TextView.显示数字时钟,会显示当前秒数。 8,图片显示控件ImageView,继承View,任何Drawable对象都可以用它来显示,注意src 属性和background属性的区别

相关文档
最新文档