因为要参加软件设计大赛么,正好选的这个题目用到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范例之4:用代码安装卸载app
  2. Android仿淘宝首页UI(附代源代码及示例图片)
  3. 如何有效的清除Android中无用的资源(静态代码分析)
  4. [置顶] Android实用代码集
  5. Android ViewPager和PagerAdapter简单代码写法
  6. Android新手入门实例之Android漂亮时钟的源代码
  7. 如何实现Android重启应用程序代码 ?

随机推荐

  1. Android(安卓)让TextView变成Dialog
  2. Android(安卓)MediaExtractor 浅析
  3. 理解Android进程创建流程(转)
  4. android 全部命令
  5. android虚拟按键NavigationBar的判断
  6. android 获取mac地址
  7. Android(安卓)字体颜色渐变效果 Span实现
  8. android AsyncTask
  9. Android(安卓)Firewall(防火墙) AndFire
  10. Android去除默认USB调试授权确认框