破手機變兒歌播放器

找到一個觸摸屏不能相應觸摸,但顯示沒有問題的手機,寫了個小程序,用來給寶寶播放兒歌。android

電腦控制手機軟件

Total Control http://tc.sigma-rt.com.cn/小程序

應用設爲開機啓動

manifest.xml文件設置以下:app

<activity
        android:name=".FullscreenActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category        android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

橫屏

按照下面代碼示例修改Activity的onResume方法dom

@Override
protected void onResume() {
 /**
  * 設置爲橫屏
  */
     if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 }
 super.onResume();
}

或者在配置文件中對Activity節點添加ide

android:screenOrientation屬性(landscape是橫向,   portrait是縱向)
android:launchMode="singleTask"  
  android:screenOrientation="portrait">

屏幕常量

view.setKeepScreenOn(true)

截獲系統音量鍵

// 攔截系統熱鍵
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int key = event.getKeyCode();
        Log.i(TAG, "[test] dispatchKeyEvent  event = " + event);
    if (key == KeyEvent.KEYCODE_VOLUME_DOWN
        || key == KeyEvent.KEYCODE_VOLUME_UP) {
        Log.i(TAG, "[test] catch  event!! return true! ");
        return true;
    }
    return super.dispatchKeyEvent(event);
}

僅響應按鍵彈起

if ( event.getAction()  == KeyEvent.ACTION_DOWN) {

               return true;// only response to key up
           }

文件/文件夾遍歷

File f = new File(Environment.getExternalStorageDirectory().getPath()+"/videohome/");
    File[] temp_dirs = f.listFiles();
    int count = 0;
    for(File dir : temp_dirs) {
        if (dir.isDirectory()) {
            count++;
        }
    }
    dirs = new File[count];

    for(File dir : temp_dirs) {
        if (dir.isDirectory()) {
            Log.e("haha",dir.toString());
            dirs[--count] = dir;
        }
    }

生成隨機數

(new Random()).nextInt( files.length);

視頻播放

this.videoView = (VideoView)this.findViewById(R.id.videoView);
    //videoView.setMediaController(new    MediaController(this));
        final MediaController mc = new MediaController(this);
    mc.setVisibility(View.INVISIBLE);
    mc.setEnabled(false);


    videoView.setMediaController(mc);

     videoView.setVideoURI(Uri.parse(file.toURI().toString()));
    videoView.start();
    videoView.requestFocus();

 videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            播放下一首
        }
 }

videoview視頻拉伸全屏

android:layout_alignParentLeft="true"
 android:layout_alignParentRight="true"
 android:layout_alignParentBottom="true"

layout改成RelativeLayoutthis

相關文章
相關標籤/搜索