I have a lyrics app that should display the lyrics of a song that is playing on the phone.
(我有一个歌词应用程序,该应用程序应显示正在播放的歌曲的歌词。)
But unfortunately it does not work, it says "loading" and does not load any lyrics. (但不幸的是,它不起作用,显示为“正在加载”,并且不加载任何歌词。)
But i tried to figure out why. (但是我试图找出原因。)
And I found that the BroadcastReceiver mReceiver is not working because it does not display the Log message in the console and I do not understand why isn't it working. (而且我发现BroadcastReceiver mReceiver无法正常工作,因为它没有在控制台中显示Log消息,而且我也不明白为什么它不能正常工作。)
Could you please help me? (请你帮助我好吗?)
Here I attached the two Java classes of the application: MainActivity.java: (在这里,我附加了应用程序的两个Java类:MainActivity.java:)
package com.example.trackslyrics;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.logging.Logger;
public class MainActivity extends AppCompatActivity {
TextView status = null;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String cmd = intent.getStringExtra("command");
Log.d("Music", cmd + " : " + action);
String artist = intent.getStringExtra("artist");
String track = intent.getStringExtra("track");
boolean playing = intent.getBooleanExtra("playing", false);
Log.d("Music", artist + " : " + track);
if (!playing) {
status.setText("No music playing");
} else {
status.setText(artist + "
" + track);
String uri = Uri.parse("http://www.stands4.com/services/v2/lyrics.php")
.buildUpon()
.appendQueryParameter("mus", track)
.appendQueryParameter("art", artist)
.build().toString();
if (uri != null) {
new VagalumeAsyncTask().execute(uri);
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter iF = new IntentFilter();
iF.addAction("com.android.music.musicservicecommand");
iF.addAction("com.android.music.metachanged");
iF.addAction("com.android.music.playstatechanged");
iF.addAction("com.android.music.updateprogress");
this.status = (TextView) this.findViewById(R.id.status);
registerReceiver(mReceiver, iF);
AudioManager manager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if (!manager.isMusicActive()) {
status.setText("No music playing");
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class VagalumeAsyncTask extends AsyncTask<String, Void, JSONObject> {
@Override
protected JSONObject doInBackground(String... params) {
JSONParser jParser = new JSONParser();
return jParser.getJSONFromUrl(params[0]);
}
@Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
try {
JSONObject mus = (JSONObject) json.getJSONArray("mus").get(0);
status.setText(mus.getString("text"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
And the JSONParser.java:
(和JSONParser.java:)
package com.example.trackslyrics;
import android.util.Log;
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.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyStore;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
HttpClient httpClient = createHttpClient();
public JSONParser() {
}
public static HttpClient createHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 15000);
HttpConnectionParams.setSoTimeout(params, 5000);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
public JSONObject getJSONFromUrl(String url) {
try {
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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, "ISO-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
ask by VladinhoRamires translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…