当前位置: 代码迷 >> Android >> android 开发札记
  详细解决方案

android 开发札记

热度:70   发布时间:2016-05-01 13:47:36.0
android 开发笔记
  我接触android快一个月了,平常做了一些练习现在我把手机最常用的功能打电话和发短信要用到的主要代码记录下来
发短信主要代码:
//构建一个default的SmsManagerSmsManager sms = new SmsManager.getDefault();PaddingIntent padding = new PaddingIntent.getBroadcast(this,0,new Intent(),0);sms.sendTextMessage("得到的电话号码",null,"发送的信息",padding,null);

打电话的主要代码:
Intent intent = new Intent("android.intent.action.DIAL",Uri.parse("tel:"+得到的电话号码));StartActivity(intent);

在androidManifest.xml
中加入如下代码:
<!--发短信服务--><uses-permission android:name="android.permission.SEND_SMS" /><!--打电话服务--><uses-permission android:name="android.permission.CALL_PHONE"/>


menu
public boolean onCreateOptionsMenu(Menu menu) {	    super.onCreateOptionsMenu(menu);		menu.add(0, ADD, 1,"添加日程").setIcon(android.R.drawable.ic_menu_add);				return true;	}	public boolean onOptionsItemSelected(MenuItem item) {				switch (item.getItemId()) {		case ADD:			showNewDiary();			return true;				}		return super.onOptionsItemSelected(item);			}




在Linux下查看数据库文件
1.cmd>adb shell
2.cd data/data
3.ls查看文件
4.进入要查看数据库的项目名
5.ls
6.cd databases
7.ls
8.sqlite3 “数据库名”
9.  .tables

AutoCompleteTextView的 用法:
//把查询出的数据存到数组里cursor=myDB.queryTile();		String[] title = new String[cursor.getCount()];		for (int i = 0; i < title.length; i++) {			cursor.moveToNext();			title[i]=cursor.getString(1);		}  //AutoCompleteTextView设置adapter    	adapter2 = new ArrayAdapter<String>(this,                android.R.layout.simple_dropdown_item_1line, title);    	autoCompleteTextView.setAdapter(adapter2);//为autoCompleteTextView设置单击事件autoCompleteTextView.setOnItemClickListener(new OnItemClickListener(){			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,					long arg3) {				Cursor autoCursor=myDB.queryByInfoId((int)arg3);				autoCursor.moveToFirst();//查询方法				mohuSelect();			}		});

ExpandableListView的用法:
//查询方法groupSelect();//设置单击事件取得id  listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {            public boolean onGroupClick(ExpandableListView parent,                            View v, int groupPosition, long id) {            	expandId=(int)id;                    // Log.i(TAG, "id:" + id);                    return false;            }       });//设置item的点击事件		listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener(){			public boolean onChildClick(ExpandableListView parent, View v,					int groupPosition, int childPosition, long id) {				Intent intent =new Intent();				intent.setClass(MyNote.this, SelectActivity.class);				Bundle bundle = new Bundle();				bundle.putInt("groupId", expandId);				bundle.putInt("subGroupId", (int)id);				intent.putExtras(bundle);				startActivity(intent);				finish();				return false;			}					}); /**按类型查询*/	public void groupSelect()	{				/*取得database里的数据*/		cursor = myDB.group_select();				adapter = new MyExpandableListAdapter(                cursor,                this,                android.R.layout.simple_expandable_list_item_2,                android.R.layout.simple_expandable_list_item_2,                new String[] {myDB.GROUP_TYPES}, // Name for group layouts                new int[] {android.R.id.text1},                new String[] {myDB.SUB_GROUPS}, // Number for child layouts                new int[] {android.R.id.text1}                );				listView.setAdapter(adapter);		cursor.requery();		listView.invalidateViews();	}//设置adapter	 public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {           	        public MyExpandableListAdapter(Cursor cursor, Context context, int groupLayout,	                int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,	                int[] childrenTo) {	            super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childrenFrom,	                    childrenTo);	        }	        @Override	        protected Cursor getChildrenCursor(Cursor subCursor) {	        	subCursor=myDB.queryByGroupId(expandId);	        	return subCursor;	        }	    } //ExpandableListView group和item长按方法  listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){			public void onCreateContextMenu(ContextMenu menu, View arg1,					ContextMenuInfo arg2) {				listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {		            public boolean onGroupClick(ExpandableListView parent,		                            View v, int groupPosition, long id) {		            	expandId=(int)id;		                    return false;		            }		       });				 ExpandableListView.ExpandableListContextMenuInfo info = 					 (ExpandableListView.ExpandableListContextMenuInfo)arg2;				  int type = ExpandableListView	                .getPackedPositionType(info.packedPosition);				if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP ) {					menu.setHeaderTitle("请选择操作类型!");					menu.add(0,0,0,"删除");					menu.add(0,1,0,"更改类别");					menu.add(0,2,0,"添加到桌面");					menu.add(0,3,0,"添加子类别");										return ;				}				else {					menu.setHeaderTitle("请选择操作类型!");					menu.add(0,4,0,"删除");					menu.add(0,5,0,"更改子类别!");					return;				}							}		});	

长按事件:
  //添加长按事件		listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){			public void onCreateContextMenu(ContextMenu menu, View arg1,					ContextMenuInfo arg2) {				menu.setHeaderTitle("请选择操作类型!");				menu.add(0,0,0,"删除");				menu.add(0,2,0,"添加到桌面");			}		});	/*对选择的事件进行操作*/	@Override	public boolean onContextItemSelected(MenuItem item) {		 //取得List位置		ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();        selectedPosition =(int)info.id;        switch(item.getItemId()){                 case 0:                        	 AlertDialog.Builder builder = new AlertDialog.Builder(MyNote.this); 		        	builder.setTitle("确定删除吗?") 		        	.setCancelable(false) 		        	.setPositiveButton("确定", new DialogInterface.OnClickListener(){ 						public void onClick(DialogInterface dialog, int which) { 							 							deleteGroup(selectedPosition); 						} 		        		 		        	}) 		        	.setNegativeButton("取消", new DialogInterface.OnClickListener(){ 						public void onClick(DialogInterface dialog, int which) { 							dialog.cancel(); 							 						} 		        		 		        	}); 		        	AlertDialog alertDialog =builder.create(); 		        	alertDialog.show();                 break;                  				case 2:				    cursor=myDB.groupById(selectedPosition);				    cursor.moveToNext();				    String groupType= cursor.getString(1);			    	  Intent addShortCut = new Intent(ACTION_ADD_SHORTCUT);				      addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);				      cursor.moveToNext();				      addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME, groupType);				      addShortCut.putExtra(Intent.ACTION_VIEW, android.R.drawable.ic_menu_help);				      addShortCut.putExtra(Intent.ACTION_MAIN, R.layout.main);				      myIntent.setData(Uri.parse(groupType));				      Bundle bundle3=new Bundle();				      bundle3.putString("group", groupType);				      myIntent.putExtras(bundle3);				      addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,				    		  myIntent);				      sendBroadcast(addShortCut); 		    		break; 		        }		return super.onContextItemSelected(item);		 	} 

往SDCard里放文件
在C:\android-sdk-windows-1.6_r1\tools里启动ddms.bat点击Device—>FileExplorer,可以看到android系统的所有目录及文件 选中sdcard,点击toolbar上的push file onto Device,选择你要上传的文件

进度条
 progressDialog = ProgressDialog.show(MediaActivity.this,"处理中...",""+findViewById(R.id.myProgress),true);                        new Thread()               {                  public void run()                  {                     try{                        // 把在显示进度条时做的动作放在这里,如 call web service                       // 这里用 sleep 来替代                   	  progressDialog.findViewById(R.id.myProgress);                  	  audiomanage.adjustVolume(AudioManager.ADJUST_RAISE, 0);                        volume=audiomanage.getStreamVolume(AudioManager.STREAM_RING);                       myProgressBar.setProgress(volume);                        mode=audiomanage.getRingerMode();                      sleep(5000);                   }                   catch(Exception e)                    {                                          }                    // 关掉进度条                    progressDialog.dismiss();                }             }.start();



<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id="@+id/lanauageShow"
    android:textSize="7pt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    />
    <EditText 
    android:id="@+id/lanauageAnswer"
    android:layout_width="fill_parent"
    android:layout_height="180px"
    android:layout_weight="1.0"
    android:editable="false"
    />
</LinearLayout>
</ScrollView>
<LinearLayout
        android:background="#808080"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="bottom">
       <TextView
        android:layout_centerInParent="true"
        android:textColor="#FFFFFF"
        android:text="@string/answer"
        android:layout_width="wrap_content"
    android:layout_height="wrap_content"/> 
    <EditText
       android:layout_centerInParent="true"
       android:id="@+id/lanauageEdit"
       android:layout_width="120px"
       android:layout_height="wrap_content"/>
    <Button
       android:layout_centerInParent="true"
       android:id="@+id/lanauageSure"
       android:text="@string/sure"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
     <Button
       android:layout_centerInParent="true"
       android:id="@+id/lanauageNext"
       android:text="@string/next"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />    
    </LinearLayout>
</FrameLayout>

Intent是Activity与Activity之间,Activity与Service之间传递参数的介质

Intent.putExtras(key, value)几乎可以包括各种类型的值,但是却没有类似List<Object>之类的传递参数

再加班无聊的时候,我试了试,发现可以把list强转成Serializable类型,然后通过putExtras(key, (Serializable)list)方法传递过去,接受的时候用(List<YourObject>) getIntent().getSerializable(key)就可以接受到List<YourObject>数据了

但是最重要的一点是:你的YourObject类必须要实现Serializable接口,切记切记,不然会报错,运行时异常(与parcel相关)

总结:保证你自己定义的类实现了Serializable接口,然后传递list的时候强转成Serializable类型,接受的时候再转换回来就可以了!
1 楼 ouyangfeng521 2011-03-28  
能发一下整个项目吗?
2 楼 xihuan&java 2011-03-30  
ouyangfeng521 写道
能发一下整个项目吗?

呵呵,我只是把日常用到的一些基础代码记录了一下,网上有很多源代码的
  相关解决方案