android应用之2

1、获取时间

      Time t= new Time();
t.setToNow();
int year = t.year;
int month = t.month;
int date = t.monthDay;
int hour = t.hour;
int minute = t.minute;
int second = t.second;

2、视频播放

a、布局文件

   <SurfaceView android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:id="@+id/surface" 
       android:layout_weight="0.25" >
   </SurfaceView>

b、java代码

MediaPlayer player;
    SurfaceView surface;
    SurfaceHolder surfaceHolder;
    AudioManager mAudioManager;
    
   OnCreate()里

       surface=(SurfaceView)findViewById(R.id.surface);
 
       surfaceHolder=surface.getHolder();//SurfaceHolder是SurfaceView的控制接口
       surfaceHolder.addCallback(this);//因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接this
//        surfaceHolder.setFixedSize(320, 220);//显示的分辨率,不设置为视频默认
       surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//Surface类型
      mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

       c、播放停止

player.start();
player.pause();
player.stop();

2、设置无标题,全屏,横竖屏

       requestWindowFeature(Window.FEATURE_NO_TITLE); //璁剧疆鏃犳爣棰�
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//鍘绘帀淇℃伅鏍�


        setContentView(R.layout.main);
        
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

3、程序开机自动运行

a、新建一个类继承broadcast类,注意这个类不能包含于主activity里,否则会报错

package android_serialport_api.sample;


import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;


public class MyInstalledReceiver extends BroadcastReceiver {
@SuppressWarnings("deprecation")
@Override

public void onReceive(Context context, Intent intent) {


if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {// install
String packageName = intent.getDataString();


Log.i("homer", "瀹夎浜� :" + packageName);
}


if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {// uninstall
String packageName = intent.getDataString();


Log.i("homer", "鍗歌浇浜� :" + packageName);
}

if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {// boot
Intent intent2 = new Intent(context, MainMenu.class);
intent2.setAction("android.intent.action.MAIN");
intent2.addCategory("android.intent.category.LAUNCHER");
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
}
}

b、在主的activity里新建上面建立的类然后再onstart里初始化,在ondestroy里注销

    private MyInstalledReceiver installedReceiver = null; //开机自启动用

    public void onStart(){
super.onStart();

installedReceiver = new MyInstalledReceiver();
IntentFilter filter = new IntentFilter();


filter.addAction("android.intent.action.PACKAGE_ADDED");
filter.addAction("android.intent.action.PACKAGE_REMOVED");
filter.addDataScheme("package");

this.registerReceiver(installedReceiver, filter);
}



protected void onDestroy() {
if(mp != null)
          mp.release();
if(installedReceiver != null) {
this.unregisterReceiver(installedReceiver);
}

if (mReadThread != null)
mReadThread.interrupt();
mApplication.closeSerialPort();
mSerialPort = null;
 if(player.isPlaying()){
       player.stop();
       }
       player.release();
       
super.onDestroy();
}

4、读取文件,与判断后缀名

a、读取文件

private String [] PictureFileName = new String[100];
    private int PictureNumber;


        File file = new File(FilePath);
       if(file.isDirectory())
       {
            File [] fileArray = file.listFiles();
            if(null != fileArray && 0 != fileArray.length)
            {
                 for(int i = 0; i < fileArray.length; i++)
                 {
                
                PictureFileName[PictureNumber++] =  FilePath + fileArray[i].getName();
                 }
            }
       }

b、判断后缀

String prefix=PictureFileName[BoFangNumber].substring(PictureFileName[BoFangNumber].lastIndexOf(".")+1); 
if(prefix.equals("jpg"))
{

5、imageview 显示SD卡里图片,注意xml里一定要整好,否则不会显示

ImageView jpgView = (ImageView)findViewById(R.id.image); 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 2; 
    Bitmap bm = BitmapFactory.decodeFile( PictureFileName[BoFangNumber], options); 
    int height = ImageSurface.getHeight();
    int width = ImageSurface.getWidth();
    bm = getReduceBitmap(bm,width,height);//将图片转化为适合控件的大小
    jpgView.setImageBitmap(bm);

6、显示和隐藏控件,根据一个控件大小设置另外一个控件大小


<LinearLayout 
    android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical">
   <SurfaceView 
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       android:id="@+id/surface" 
       android:visibility="gone" >
   </SurfaceView>
   <ImageView
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       android:id="@+id/image"
       >
   </ImageView>
       </LinearLayout>

//以上两个控件同一时刻只能显示一个

ImageView jpgView = (ImageView)findViewById(R.id.image); 
int height = ImageSurface.getHeight();
    int width = ImageSurface.getWidth();
jpgView.setVisibility(View.GONE);
surface.setVisibility(View.VISIBLE);
LayoutParams lp = surface.getLayoutParams();
lp.width = width;
lp.height =height;
surface.setLayoutParams(lp);

7、关于自动布局问题

<LinearLayout 
    android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical">
   <SurfaceView 
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       android:id="@+id/surface" 
       android:visibility="gone" >
   </SurfaceView>
   <ImageView
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       android:id="@+id/image"
       >
   </ImageView>
       </LinearLayout>

  a、当 android:layout_height="fill_parent"时

 android:layout_weight="1"占小的比例

 b、当为0dp的时候占的是大比例,

 c、父方向是哪个方向,就代表是哪个方向上面的


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值