因为要参加软件设计大赛么,正好选的这个题目用到android的知识,所以就硬着头皮学了。通过学习的一些知识,我试着做了一个android的简单的播放器,虽然很简单,但是做的过程中还是遇到了很多困难和问题,不过经过一定的修改,还是运行成功了,同时还把知识掌握的更牢靠了。以下是我的代码:
(1)android说白了,同样也用到了MVC的框架,比如说下面的这部分就是V部分,同时我认为这也是我们开发这种程序应该做的第一步 :layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
android:id="@+id/Button_start"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop"
android:id="@+id/Button_stop"
>
</Button>
</LinearLayout>
(2)这里可以看作是C部分,activity和service部分的代码
package com.test.activity;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
package com.test.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
//当然这里你也可以声明OnClickListener接口来写,我只是提供了一种写法
public class MusicActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button) this.findViewById(R.id.Button_start);
//估计是这里出错了,你点击的时候肯定是从视图里取了么, 切记是new View
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startService(new Intent("com.test.activity.auto_player"));
}
});
Button button2=(Button) this.findViewById(R.id.Button_stop);
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
stopService(new Intent("com.test.activity.auto_player"));
MusicActivity.this.finish();
}
});
}
}
package com.test.activity;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service {
private MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
player=MediaPlayer.create(this,R.raw.test);
player.start();
}
}
(3)这部分其实很重要,很多东西都得在这里声明了才能运行:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MusicActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MusicService">
<intent-filter>
<action android:name="com.test.activity.auto_player"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
(4)当然以上这部分就是主要的部分,还有一部分就不在这里列出了,只希望能与大家共勉!
(1)android说白了,同样也用到了MVC的框架,比如说下面的这部分就是V部分,同时我认为这也是我们开发这种程序应该做的第一步 :layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
android:id="@+id/Button_start"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop"
android:id="@+id/Button_stop"
>
</Button>
</LinearLayout>
(2)这里可以看作是C部分,activity和service部分的代码
package com.test.activity;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
package com.test.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
//当然这里你也可以声明OnClickListener接口来写,我只是提供了一种写法
public class MusicActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button) this.findViewById(R.id.Button_start);
//估计是这里出错了,你点击的时候肯定是从视图里取了么, 切记是new View
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startService(new Intent("com.test.activity.auto_player"));
}
});
Button button2=(Button) this.findViewById(R.id.Button_stop);
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
stopService(new Intent("com.test.activity.auto_player"));
MusicActivity.this.finish();
}
});
}
}
package com.test.activity;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service {
private MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
player=MediaPlayer.create(this,R.raw.test);
player.start();
}
}
(3)这部分其实很重要,很多东西都得在这里声明了才能运行:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MusicActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MusicService">
<intent-filter>
<action android:name="com.test.activity.auto_player"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
(4)当然以上这部分就是主要的部分,还有一部分就不在这里列出了,只希望能与大家共勉!