編輯:關於Android編程
案例:
<Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:onClick="doStart"
android:text="Start Service"/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/btn_stop"
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:onClick="doStop"
android:text="Start Service"/>
public void doStart(View view){
Intent intent = new Intent(this,SampleService.class);
startService(intent);
}
public void doStop(View view){
Intent intent = new Intent(this,SampleService.class);
stopService(intent);
}
public class SampleService exteds Service{
public void onCreate(){
Log.d(tag,"onCreate")
super.onCreate();
}
public int onStartCommand(Intent intent,int flags,int startId){
Log.d(tag,"onStartCommand");
return super.onStartCommand(intent,flags,startId);
}
public void onDestroy(){
Log.d(tag,"onDestroy");
super.onDestroy();
}
}
AndroidMainfest.xml案例:
MainActivity:
package com.edu.hpu.service;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startMusic(View view){
Intent intent = new Intent(this,PlayerMusicService.class);
startService(intent);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
Intent intent = new Intent(this,PlayerMusicService.class);
stopService(intent);
super.onDestroy();
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="136dp"
android:onClick="startMusic"
android:text="播放音樂" />
</RelativeLayout>
Service類:
package com.edu.hpu.service;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;
public class PlayerMusicService extends Service{
private MediaPlayer player;
@Override
public void onCreate() {
try {
player = new MediaPlayer();
player.reset();
player.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Music/Groove Coverage - She.mp3");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO: handle exception
e.printStackTrace();
}catch(SecurityException e){
e.printStackTrace();
}
catch(IllegalStateException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
player.release();
player = null;
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
配置:
關於MediaPlayer MediaPlayer支持主流的音頻、視頻文件的播放,亦支持播放 非本機的媒體文件; MediaPlayer會開啟子線程播放歌曲; 可調用pause()方法暫停播放,調用seekTo()方法快進到指定的 位置開始播放; 可調用prepareAsync()方法加載歌曲,並配置OnPreparedListener, 在監聽器用調用MediaPlayer的start()方法; 通常為MediaPlayer配置OnCompletionListener,以實現在播放完成後的處理
Android DrawerLayout帶有側滑功能的布局類(1)
DrawerLayout顧名思義就是一個管理布局的。使用方式可以與其它的布局類類似。DrawerLayout帶有滑動的功能。只要按照drawerLayout的規定布局方式
android之官方下拉刷新組件SwipeRefreshLayout
一、問題描述 在android開發中,使用最多的數據刷新方式就是下拉刷新了,而完成此功能我們使用最多的就是第三方的開源庫PullToRefresh。現如今,goo
Android doc Training部分 --Building a Flexible UI(部分翻譯)
To create a dynamic and multi-pane user interface on Android, you need to encapsulate
Android調用系統相冊和相機選擇圖片並顯示在imageview中
Android調用系統相冊和相機選擇圖片並顯示在imageview中,在系統調用相機拍攝中,直接返回的是經過壓縮處理後的圖像,當你直接把返還後的圖片放在imageview