没有合适的资源?快使用搜索试试~ 我知道了~
首页android studio 3.0 service项目背景音乐实现
资源详情
资源评论
资源推荐
android studio 3.0 service项目背景音乐实现项目背景音乐实现
主要介绍了android studio 3.0中service项目实现插入背景音乐的方法。
这篇文章是博主在通过学习Android Studio的同时,实现service项目中用于背景音乐的实现,邮件的发送用于随堂小测的发送邮件功能。其中也碰到需要坑和错误,最后都解决了,一起跟着学习
一下吧。如果大家有更好的方法可以在下面的留言区讨论。
本次项目我主要负责Android studio的后端,以及游戏文案游戏策划,结果后来事情太散了,Android studio学的不咋地,文案写完还有帮着写一写数据库的插入语句,然后就是跟队友完成了as的
后台插入声音的代码。接下来介绍的service项目中用于背景音乐的实现,邮件的发送用于随堂小测的发送邮件。
开发基础之Service
Activity可以呈现一个用户界面,但是Service运行在后台,试了以下实例,启动Service,并通过从Activity向Service传递数据。
package com.example.lhb.startservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.view.ViewDebug;
import android.widget.Toast;
public class MyService extends Service {
private boolean Running=false;
private String data="默认信息!!!";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
data=intent.getStringExtra("data");//这里的intent是参数里的,不是自定义的
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
Running=true;
new Thread(){
@Override
public void run() {
super.run();
while (Running){
System.out.println(data);
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void onDestroy() {
super.onDestroy();
Running=false;
}
}
//主代码
package com.example.lhb.startservice;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private EditText inputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputText= (EditText) findViewById(R.id.inputText);
if(inputText.getText().length()==0){
Toast.makeText(MainActivity.this,"请输入传递的值!",Toast.LENGTH_SHORT).show();
return;
}
Intent intent;
intent=new Intent(MainActivity.this,MyService.class);
intent.putExtra("data",inputText.getText().toString());
startService(intent);
}
});
findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent;
intent=new Intent(MainActivity.this,MyService.class);
stopService(intent);
}
});
}
}
以此来完成Activity向Service传递数据的任务。
之后尝试了as中间去实现音乐播放器,参考第一行代码p303-307。
先写入布局代码,三个按钮用来播放,停止,暂停
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
weixin_38509504
- 粉丝: 1
- 资源: 952
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- stc12c5a60s2 例程
- Android通过全局变量传递数据
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0