Android-service
2015-08-10 08:51
344 查看
MainActivity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button playButton = null;
private Button stopButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = (Button)findViewById(R.id.startPlayButton);
stopButton = (Button)findViewById(R.id.stopPlayButton);
playButton.setOnClickListener(new ButtonListen());
stopButton.setOnClickListener(new ButtonListen());
}
class ButtonListen implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.startPlayButton:
startService(new Intent(MainActivity.this, MyService.class));
break;
case R.id.stopPlayButton:
stopService(new Intent(MainActivity.this, MyService.class));
break;
default:
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyService
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer player = null;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this,
"MyService Createed!",
Toast.LENGTH_SHORT).show();
player = MediaPlayer.create(this, R.raw.burning);
player.setLooping(false);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this,
"MyService onStart!",
Toast.LENGTH_SHORT).show();
player.start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
Toast.makeText(this,
"MyService onDestroy",
Toast.LENGTH_SHORT).show();
}
}
Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second" >
</activity>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" >
</service>
</application>
</manifest>
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/startPlayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始播放"
/>
<Button
android:id="@+id/stopPlayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止播放"
/>
</LinearLayout>
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button playButton = null;
private Button stopButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = (Button)findViewById(R.id.startPlayButton);
stopButton = (Button)findViewById(R.id.stopPlayButton);
playButton.setOnClickListener(new ButtonListen());
stopButton.setOnClickListener(new ButtonListen());
}
class ButtonListen implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.startPlayButton:
startService(new Intent(MainActivity.this, MyService.class));
break;
case R.id.stopPlayButton:
stopService(new Intent(MainActivity.this, MyService.class));
break;
default:
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyService
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer player = null;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this,
"MyService Createed!",
Toast.LENGTH_SHORT).show();
player = MediaPlayer.create(this, R.raw.burning);
player.setLooping(false);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this,
"MyService onStart!",
Toast.LENGTH_SHORT).show();
player.start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
Toast.makeText(this,
"MyService onDestroy",
Toast.LENGTH_SHORT).show();
}
}
Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second" >
</activity>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" >
</service>
</application>
</manifest>
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/startPlayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始播放"
/>
<Button
android:id="@+id/stopPlayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止播放"
/>
</LinearLayout>
相关文章推荐
- android precelable和Serialization序列化数据传输
- Android自定义组合控件的实现
- Android Api Demos登顶之路(二十六)Action Bar Tabs
- 【Android进阶篇】自定义应用权限
- Android AndroidManifest 清单文件以及权限详解
- Android中Bitmap,byte[],Drawable相互转化
- Android百分比布局支持库介绍——com.android.support:percent
- android <xliff:g> 疑惑
- Android SDK 201508可用的镜像下载
- 高仿SinaWeibo新浪微博发布页面话题效果
- Android 编译报错HashSet_jni.h
- android获取屏幕尺寸、密度(判断手机屏幕类型)
- Android核心基础-8.Android四大组件之Activity-2.关闭、传递数据
- Android Binder理解
- Android中几种dialog的实现
- 【Android开源项目分析】TAB导航栏PagerSlidingTabStrip的使用和源码分析
- android动画(Animation)
- mac android studio NDK 安装使用(不用翻墙)
- Android进阶之采用TabHost和RadioButton实现底部页面导航
- android笔记--Intent和IntentFilter详解