评论

收藏

[其他] android Service

网络安全 网络安全 发布于:2021-08-06 13:15 | 阅读数:339 | 评论:0

效果:点击start 按钮,开始播放音乐,点击end 按钮,暂停音乐,手机按home键,音乐依旧进行。
LocalService.java
package com.example.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class LocalService extends Service{
private MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
player.stop();
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
player=MediaPlayer.create(this, R.raw.home);
player.start();
return super.onStartCommand(intent, flags, startId);
}
}
MainActivity.java

package com.example.servicedemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button start,end;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(Button)findViewById(R.id.start);
end=(Button)findViewById(R.id.end);
start.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
      Intent intent=new Intent("intent.LocalService");
      startService(intent);
      }
    });
end.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
      Intent intent=new Intent("intent.LocalService");
      stopService(intent);
      }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.servicedemo"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name="com.example.servicedemo.MainActivity"
      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=".LocalService">
      <intent-filter>
        <action android:name="intent.LocalService"></action>
        <category android:name="android.intent.category.DeFAULT"></category>
      </intent-filter>
    </service>
  </application>
</manifest>





关注下面的标签,发现更多相似文章