因为要参加软件设计大赛么,正好选的这个题目用到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. 《ZigBee开发笔记》第六部分 项目篇 基于ZigBee和Openwrt的智能
  2. Android第七期 - 二维码扫描与生成图
  3. android部分BUG
  4. android飞翔的小鸟……
  5. Android(安卓)小知识点
  6. android TextView 设置部分文本 边框和样式
  7. android inputreader 部分对event数据的处理
  8. android 多媒体部分学习笔记十--简单视频播放
  9. android clipPath切割画布

随机推荐

  1. Android多窗口的实现 - 开源
  2. android Sqlite3 相同sql 在命令行和数据
  3. Android插入USB设备,自动弹出提示运行apk
  4. 基于 SQLite 开发Android(安卓)studio 的
  5. Flutter 新闻客户端 - 09 详情页展示、分
  6. 新建Android,OpenGIS,Maven2等8个圈子,欢
  7. android crash 调试
  8. Android(安卓)Studio的各种冷知识,黑科技
  9. 让Android应用程序申请获取ROOT权限
  10. Android中service的使用