//这是我服务的代码
#include <thread>
void func() {
}
void fail1() {
std::thread t(func);
// will fail when we try to destroy t since it is not joined or detached
}
void works1() {
std::thread t(func);
t.join();
}
void works2() {
std::thread t(func);
t.detach();
}
int main() {
// fail1();
// works1();
// works2();
}
//启动服务的点击事件。
public class MyService extends Service {
MediaPlayer media;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
media=MediaPlayer.create(this, R.raw.song);
stopService(intent);
return null;
}
@Override
public void onCreate() {
media=MediaPlayer.create(this, R.raw.song);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
media.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
media.start();
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
// media.stop();
media.release();
super.onDestroy();
}
public void dataParse(String value){
if(value.equals(R.string.play)){
media.start();
}else if(value.equals("pause")) {
media.pause();
}else {
media.stop();
}
}
}
//现在的问题是我正在播放时播放正常,但我会尝试使用以下异常暂停应用崩溃。但是当服务正在运行mp3播放时。
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(tv.getText().toString().equals(getString(R.string.play))){
startService(intent);
tv.setText(R.string.pause);
}else if(tv.getText().toString().equals(getString(R.string.pause))){
myService.dataParse(getString(R.string.pause));
tv.setText(R.string.resume);
}else {
myService.dataParse(getString(R.string.stop));
}
}
});
//我被困了几次。请提供一些解决方案。
答案 0 :(得分:1)
if(mediaplayer != null) {
mediaplayer.stop();
mediaplayer.reset();
mediaplayer.release();
}
答案 1 :(得分:0)
public void dataParse(String value){
if(value.equals(R.string.play)){
if(!media.isPlaying())
{
media.start();
}
}else if(value.equals("pause")) {
if(media.isPlaying())
{
media.pause();
}
}else {
if(media.isPlaying())
{
media.stop();
}
}
}
还可以停止服务以暂停媒体,然后再次启动服务
stopService(intent);
答案 2 :(得分:0)
package com.android.homeworthyinspection.globaldata;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
InputStream is ;
JSONObject jObj;
String json;
// constructor
public JSONParser() {
is = null;
jObj = null;
json = "";
}
public JSONObject getJSONFromUrl(String url, JSONObject jsonObject) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
//Log.e("param",params +"");
httpPost.setHeader("Content-type", "application/json");
StringEntity se = new StringEntity(jsonObject.toString());
httpPost.setEntity(se);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", ">"+json);
} catch (Exception e) {
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
// Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
public String POST(String url, JSONObject person){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
// JSONObject jsonObject = new JSONObject();
/*jsonObject.accumulate("name", person.getName());
jsonObject.accumulate("country", person.getCountry());
jsonObject.accumulate("twitter", person.getTwitter());*/
// 4. convert JSONObject to JSON to String
json = person.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(person.toString());
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
public JSONObject getJSONFromUrl1(String url,JSONObject nameValuePairs) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
//Log.e("param",params +"");
httpPost.setHeader("Content-type", "application/json");
/*if(params!=null)*/
/*httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
httpPost.setEntity(new UrlEncodedFormEntity(params));*/
StringEntity se = new StringEntity(nameValuePairs.toString(),"UTF-8");
//sets the post request as the resulting string
httpPost.setEntity(se);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
//Log.e("JSON", ">"+json);
} catch (Exception e) {
Log.i("exc at json parser", ""+e);
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
// Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
/*public JSONObject getJSONFromUrl(String url_, JSONObject jsonObject) {
try {
URLConnection urlConn;
DataOutputStream printout;
URL url = new URL (url_);
urlConn = url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type","application/json");
urlConn.connect();
urlConn.setConnectTimeout(300000000);
Log.e("jsonobject",jsonObject +"");
printout = new DataOutputStream(urlConn.getOutputStream ());
printout.writeBytes(jsonObject.toString());
printout.flush ();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
printout.close ();
reader.close();
json = sb.toString();
Log.e("JSON", ">"+json);
} catch (Exception e) {
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
// Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}*/
}