在Android应用中添加一个图标到通知区域

时间:2013-01-02 17:09:19

标签: java android notifications android-mediaplayer

O.K我已经浏览了这个网站和其他人,我看到类似于我想要的东西,但它不是我想要的...让我解释一下 我正在学习使用eclipse构建一个Android应用程序,其中有一个媒体播放器链接到我正在运行的shoutcast服务器,到目前为止应用程序工作得很好但顶部没有图标是通知区域,当我去一个不同的应用程序(一个游戏)播放,同时离开音乐播放它最小化应用程序..这很好,但当我重新打开应用程序,它不会停止媒体播放器,如果我玩玩家我会有2个玩家运行我会有使用任务管理器强制停止它,我知道这可以完成,因为我的手机上的应用程序有图标,它会做我想要的 当我搜索这个网站时,我发现了一个指向youtube的链接,它有类似于我想要的东西 http://www.youtube.com/watch?v=e74z0_Z5QWI&feature=relmfu显示的代码如下

import android.app.Activity;

public class StatusBar extends Activity implements OnclickListener{

NotificationManager nm;


@Override
Protected void onCreate(bundle savedInstance){
//TODO Auto-generated method Stub
    super.oncreate(savedInstaceState);
    setContentView(R.layout.statusbar);
    Button stat = (Button)findViewById(R.id.bStatusBar);
    stat.setOnClickListener(this);
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }
public void onClick(View v){
//TODO Auto-generated method Stub
    Intent intent = new Intent(this, StatusBar.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

}

我已将此代码添加到我的MainActivity.java中,但它向我发出了几个错误,我试图使用quickfix修复此处是我目前在MainActivity.java文件中的脚本

package com.myapp.mytapp;


import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;


public class MainActivity extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
    private Button btn_play,
    btn_pause,
    btn_stop;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int lengthOfAudio;
private final String URL = "http://link to url:8002";
private final Handler handler = new Handler();
private final Runnable r = new Runnable() { 
@Override
public void run() {
updateSeekProgress();                   
}
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}

private void init() {
btn_play = (Button)findViewById(R.id.btn_play);
btn_play.setOnClickListener(this);
btn_pause = (Button)findViewById(R.id.btn_pause);
btn_pause.setOnClickListener(this);
btn_pause.setEnabled(true);
btn_stop = (Button)findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(this);
btn_stop.setEnabled(true);

seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);

mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);

}

@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
seekBar.setSecondaryProgress(percent);
}

@Override
public void onCompletion(MediaPlayer mp) {
btn_play.setEnabled(true);
btn_pause.setEnabled(false);
btn_stop.setEnabled(false);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (mediaPlayer.isPlaying()) {
SeekBar tmpSeekBar = (SeekBar)v;
mediaPlayer.seekTo((lengthOfAudio / 100) * tmpSeekBar.getProgress() );
}
return false;
}

@Override
public void onClick(View view) {

try {
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
lengthOfAudio = mediaPlayer.getDuration();
} catch (Exception e) {
//Log.e("Error", e.getMessage());
}

switch (view.getId()) {
case R.id.btn_play:
playAudio();
break;
case R.id.btn_pause:
pauseAudio();
break;
case R.id.btn_stop:
stopAudio();
break;
default:
break;
}

updateSeekProgress();
}

private void updateSeekProgress() {
if (mediaPlayer.isPlaying()) {
seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / lengthOfAudio) * 100));
handler.postDelayed(r, 1000);
}
}

private void stopAudio() {
mediaPlayer.stop();
btn_play.setEnabled(true);
btn_pause.setEnabled(true);
btn_stop.setEnabled(true);
seekBar.setProgress(0);
}

private void pauseAudio() {
mediaPlayer.pause();
btn_play.setEnabled(true);
btn_pause.setEnabled(true);
}

private void playAudio() {
mediaPlayer.start();
btn_play.setEnabled(true);
btn_pause.setEnabled(true);
btn_stop.setEnabled(true);
}

public void homepage (View view) {
    goToUrl ( "http://myhomepage.com/");
}

private void goToUrl (String url) {
    Uri uriUrl = Uri.parse(url);
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
    startActivity(launchBrowser);
}

}

这是我的activity_main.xml文件中的内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black"
    android:icon="@drawable/ic_launcher"
    android:orientation="vertical" 
    startForeground()>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logo" />

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >


        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
                   <Button
            android:id="@+id/homepage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:clickable="true"
            android:cursorVisible="true"
            android:linksClickable="true"
            android:onClick="homepage"
            android:text="Home Page" />



        </TableRow>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_play"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:background="@drawable/play"
            android:clickable="true"
            android:cursorVisible="true"
            android:src="@drawable/play" >
        </Button>

        <Button
            android:id="@+id/btn_pause"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:background="@drawable/pause"
            android:clickable="true"
            android:cursorVisible="true"
            android:src="@drawable/pause" >
        </Button>

        <Button
            android:id="@+id/btn_stop"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:background="@drawable/stop"
            android:clickable="true"
            android:cursorVisible="true"
            android:src="@drawable/stop" >
        </Button>
    </LinearLayout>

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="3dp" >
    </SeekBar>

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/scast" />

    <TextView
        android:id="@+id/Textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="center"
        android:hint="@string/version" >
    </TextView>

</LinearLayout>

有人可以告诉我我做错了什么或者可以改写我所拥有的并且仍然保持一切正常 提前致谢 罗布

0 个答案:

没有答案