1

I am program Music Play in WebView getting link from my website.

I want my WebView activity kept with Status Bar Notification when user pressed Home Button. Please help give me some way.

My Code in WebView:

public class playlist extends Activity {
private WebView mWebView;
MediaPlayer myMediaPlayer;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);

    mWebView = new WebView(this);

    mWebView = (WebView) findViewById(R.id.playlist);
    mWebView.setWebViewClient(new myWebClient());
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.loadUrl("http://www.mp3skull.com/justin_bieber.html");
}

public class myWebClient extends WebViewClient  
{  
    @Override  
    public void onPageStarted(WebView view, String url, Bitmap favicon) {  
        // TODO Auto-generated method stub  
        super.onPageStarted(view, url, favicon);  
    }  
    @Override  
    public boolean shouldOverrideUrlLoading(WebView view, String url) {  
        // TODO Auto-generated method stub  
        view.loadUrl(url);  
        return true;
    }  
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
@Override
public void onPause() {
    mWebView.setVisibility(View.GONE);
    mWebView.destroy();
    super.onPause();
}
}

This code when user press Home Button the Music also stop because of Voide onPause(), but I need keep music playing on Status Bar Notification with correct activity. Please help me.

Regards, Virak

SopheakVirak
  • 961
  • 6
  • 14
  • 36

1 Answers1

1

You can use onUserLeaveHint () method for playing sound when user press Home Key as doc says:

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.

For playing sound on Home key press use IntentService. start it in onUserLeaveHint() of your Activity and this service auto stop when job is finished.as:

public class MyService extends IntentService {

public MyService(String name) {
    super(name);
    // TODO Auto-generated constructor stub
}
public MyService () {
  super("MyService");
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
protected void onHandleIntent(Intent arg0) {
    // TODO Auto-generated method stub
         // PLAY SOUND HERE
    }
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213