几年前,我制作了一个android radio应用程序(api 8),该应用程序可以播放在线广播电台并显示来自mysql数据库的信息 我使用xml来显示有关Text和ImageView的信息,并使用计时器
更新了数据现在,我想在离线一段时间后将我的应用程序更新为api 15,但是saxparser在api 15上不再起作用,所以我按照教程进行操作,并使用listview json和customlistadapter更新了我的应用程序
仅现在,我不知道如何自动刷新“正在播放”信息。 我尝试了很多东西,搜索了许多教程,但仍然无法弄清楚如何用我的新代码实现这一目标。
这是我与saxparser一起使用的旧代码:
private class UpdateTimeTask implements Runnable {
@Override
public void run() {
try {
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL(
"http://www.example.com/linktodata.php");
XMLHandler myXMLHandler = new XMLHandler();
xmlR.setContentHandler(myXMLHandler);
xmlR.parse(new InputSource(url.openStream()));
data = XMLHandler.data;
for (int i = 0; i < data.getTitle().size(); i++) {
Lyrics.setText(data.getLyric().get(i));
myimageURL = data.getPic().get(i);
Song.setText(data.getTitle().get(i));
Song.setTextColor(Color.parseColor("#7cfc00"));
frAlbum.setTextColor(Color.parseColor("#cd853f"));
Artist.setText(data.getArtist().get(i));
Artist.setTextColor(Color.parseColor("#000000"));
Album.setText(data.getAlbum().get(i));
Album.setTextColor(Color.parseColor("#ff0000"));
nowplaying.setText("Now Playing...");
nowplaying.setTextColor(Color.parseColor("#cd853f"));
}
downloadFile(myimageURL);
} catch (Exception e) {
e.printStackTrace();
}
}
Bitmap bmImg;
void downloadFile(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
if (bmImg != null) {
AlbumPic.setImageBitmap(bmImg);
AlbumPic.clearAnimation();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m_handler.postDelayed(m_updateTime, DELAY);
// timer end
}
}
这就是我现在拥有的 我的MainActivity:
public class MainActivity extends Activity implements View.OnClickListener,
SeekBar.OnSeekBarChangeListener {
// Log tag
private static final String TAG = "ServicesDemo";
static public SeekBar sb;
public static ToggleButton playStop;
static TextView tvBuffer;
// Music json url
private static final String url = "http://www.example.com/linktodata.php";
private ProgressDialog pDialog;
private List<Music> musicList = new ArrayList<Music> ();
private ListView listView;
private CustomListAdapter adapter;
private UpdateTimeTask m_updateTime;
private Handler m_handler;
public AudioManager am;
/**
* The delay in milliseconds between updates.
*/
private final int DELAY = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
listView = (ListView) findViewById ( R.id.list );
adapter = new CustomListAdapter ( this, musicList );
listView.setAdapter ( adapter );
pDialog = new ProgressDialog ( this );
// Showing progress dialog before making http request
pDialog.setMessage ( "Loading..." );
pDialog.show ();
tvBuffer = findViewById ( R.id.tvBuffer );
playStop = findViewById ( R.id.toggPlayStop );
playStop.setBackgroundResource ( R.drawable.stopbtn );
sb = findViewById ( R.id.volumCntrl );
am = (AudioManager) getSystemService ( Context.AUDIO_SERVICE );
int maxV = am.getStreamMaxVolume ( AudioManager.STREAM_MUSIC );
int curV = am.getStreamVolume ( AudioManager.STREAM_MUSIC );
sb.setMax ( maxV );
sb.setProgress ( curV );
sb.setOnSeekBarChangeListener ( this );
playStop.setOnClickListener ( this );
tvBuffer.setVisibility(View.INVISIBLE);
tvBuffer.setTextColor( Color.parseColor("#ff0000"));
// changing action bar color
// Creating volley request obj
JsonArrayRequest musicReq = new JsonArrayRequest ( url,
new Response.Listener<JSONArray> () {
@Override
public void onResponse(JSONArray response) {
Log.d ( TAG, response.toString () );
hidePDialog ();
// Parsing json
for (int i = 0; i < response.length (); i++) {
try {
JSONObject obj = response.getJSONObject ( i );
Music music = new Music ();
music.setTitle ( obj.getString ( "title" ) );
music.setThumbnailUrl ( obj.getString ( "picture" ) );
music.setAlbum ( obj.getString ( "album" ) );
music.setLyrics ( obj.getString ( "lyrics" ) );
music.setArtist ( obj.getString ( "artist" ) );
// adding music to music array
musicList.add ( music );
} catch (JSONException e) {
e.printStackTrace ();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged ();
}
}, new Response.ErrorListener () {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d ( TAG, "Error: " + error.getMessage () );
hidePDialog ();
}
} ); // Adding request to request queue
AppController.getInstance().addToRequestQueue(musicReq);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@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;
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart ();
try {
if (UtilityClass.isInternetOn ( this )) {
m_updateTime = new UpdateTimeTask ();
m_handler = new Handler ();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace ();
}
playStop.setChecked ( false );
} private class UpdateTimeTask implements Runnable {
@Override
public void run() {
try {
adapter.notifyDataSetChanged ();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace ();
}m_handler.post ( m_updateTime );
}
}
@Override
protected void onResume() {
super.onResume ();
// The activity has become visible (it is now "resumed").
new PlayerStatusCheck ().execute ();
}
//AsyncTask Class
class PlayerStatusCheck extends AsyncTask<Object, Object, Object> {
boolean isServiceAvailable;
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService ( Context.ACTIVITY_SERVICE );
for (ActivityManager.RunningServiceInfo service : manager
.getRunningServices ( Integer.MAX_VALUE )) {
if (com.example.my.My_Service.class.getName ()
.equals ( service.service.getClassName () )) {
return true;
}
}
return false;
}
@Override
protected Object doInBackground(Object... params) {
isServiceAvailable = isMyServiceRunning ();
return null;
}
@Override
protected void onPostExecute(Object result) {
if ((isServiceAvailable)) {
playStop.setChecked ( true );
playStop.setBackgroundResource ( R.drawable.stopbtn );
tvBuffer.setVisibility ( View.VISIBLE );
if (My_Service.mediaPlayer.isPlaying ()) {
playStop.setBackgroundResource ( R.drawable.playbtn );
}
} else {
playStop.setChecked ( false );
}
}
}
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
// TODO Auto-generated method stub
am.setStreamVolume ( AudioManager.STREAM_MUSIC, progress, 0 );
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (playStop.isChecked() && UtilityClass.isInternetOn(this)) {
tvBuffer.setVisibility( View.VISIBLE);
playStop.setBackgroundResource(R.drawable.playbtn);
startService(new Intent (this, My_Service.class));
} else if (UtilityClass.isInternetOn(this)) {
stopService(new Intent(this, My_Service.class));
playStop.setBackgroundResource(R.drawable.stopbtn);
}
}
}
CustomListAdapter
public class CustomListAdapter extends BaseAdapter {
TextView title;
TextView album;
TextView artist;
TextView lyrics;
NetworkImageView thumbNail;
private Activity activity;
private LayoutInflater inflater;
private List<Music> musicItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Music> musicItems) {
this.activity = activity;
this.musicItems = musicItems;
}
@Override
public int getCount() {
return musicItems.size();
}
@Override
public Object getItem(int location) {
return musicItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
thumbNail = convertView
.findViewById(R.id.thumbnail);
title = convertView.findViewById(R.id.title);
album = convertView.findViewById(R.id.album);
artist = convertView.findViewById(R.id.artist);
lyrics = convertView.findViewById(R.id.lyrics);
// getting music data for the row
Music m = musicItems.get(position);
// thumbnail image
thumbNail.setImageUrl("https://example.com/album_art/" + m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
// rating
album.setText(String.valueOf(m.getAlbum ()));
// genre
artist.setText ( m.getArtist () );
// release year
lyrics.setText((m.getLyrics ()));
return convertView;
}
}
音乐对象数据:
public class Music {
private String title;
private String lyrics;
private String album;
private String artist;
private String thumbnailUrl;
public Music() {
}
public Music(String title, String thumbnailUrl, String lyrics, String album, String artist)
{
this.title = title;
this.thumbnailUrl = thumbnailUrl;
this.lyrics = lyrics;
this.album = album;
this.artist = artist;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = name;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public String getLyrics() {
return lyrics;
}
public void setLyrics(String lyrics) {
this.lyrics = lyrics;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getArtist() {
return artist;
}
public void setArtist (String artist) {
this.artist = artist;
}
}
我希望有人能帮助我。 这是完成应用程序的最后一步。
答案 0 :(得分:0)
您可以通过这种方式实现。
在您的boolean
类中参加Music
,以完成任务。播放时将其分配给true
,其他所有对象将变为false
。
现在检查boolean
适配器类中的CustomListAdapter
。
如果是真的,则显示您的Now Playing
信息文本,否则将其隐藏。
当您将boolean
分配给特定的false
并在其后调用true
时,请不要忘记将其他所有adapter.notifyDataSetChanged()
设为public class CustomListAdapter extends BaseAdapter {
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
// getting music data for the row
Music m = musicItems.get(position);
...
if(m.getIsPlaying())
-show playNow text.
else
-hide it.
...
return convertView;
}
}
。
所以你的代码看起来像这样...
public class Music {
...
private boolean isPlaying;
public Music() {
}
public Music(String title, String thumbnailUrl, String lyrics, String album, String artist, boolean isPlaying)
{
this.title = title;
this.thumbnailUrl = thumbnailUrl;
this.lyrics = lyrics;
this.album = album;
this.artist = artist;
this.isPlaying = isPlaying.
}
...
public String getIsPlaying() {
return isPlaying;
}
public void setIsPlaying(boolean isPlaying) {
this.isPlaying = isPlaying;
}
}
音乐对象类如下所示。
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $query ) {
$query->set( 'meta_query', array( array(
'key' => '_stock',
'value' => '5',
'compare' => '>'
)));
}