0

当我点击这个Widget收音机应该开始播放,当我再次点击它时,收音机应该停止。

这是我的代码~!

private RemoteViews buildUpdate(Context context) {
            RemoteViews updateViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget);   
     player =new MediaPlayer();
         try {
            player.setDataSource("http://94.102.55.40:8123");
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         player.prepareAsync();
         player.start();
     if(player.isPlaying()){
            player.stop();
        updateViews.setImageViewResource(R.id.phoneState,
                        R.drawable.on);
         }else{
        player.prepareAsync();
        player.start();
        updateViews.setImageViewResource(R.id.phoneState,
                        R.drawable.off);        
          }
    Intent i = new Intent(this, Appwidget.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    updateViews.setOnClickPendingIntent(R.id.phoneState, pi);
    return updateViews;
}
4

0 回答 0