Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Android
MainActivity is not binding with service
shadman kudchikar
Greenhorn
Posts: 13
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This is my main activity
package com.downloadsongsdownload.musiqa; import java.util.concurrent.TimeUnit; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.MediaMetadataRetriever; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.support.v4.view.GestureDetectorCompat; import android.view.GestureDetector; import android.view.GestureDetector.OnDoubleTapListener; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.TextView; import com.downloadsongsdownload.musiqa.MusicService.MusicBind; @SuppressLint("NewApi") public class MainActivity extends Activity implements GestureDetector.OnGestureListener, OnDoubleTapListener { //constant and member use by activity private static final float SWIPE_VELOCITY_THRESHOLD = 100; private static final float SWIPE_THRESHOLD = 100; private ImageView coverart; private Button b1,b2,b3,b4; Intent playIntent; MusicService musicService; boolean serviceConn=false; private Handler myHandler = new Handler(); private SeekBar seekbar; private TextView tx1,tx2; GestureDetectorCompat gd; // a service connection object for bindservice method that binds music player with layout ServiceConnection conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.musiqa); conn=new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName arg0) { serviceConn=false; } @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { MusicBind musicBind=(MusicBind) arg1; musicService=musicBind.getService(); serviceConn=true; } }; //start method that bind music service with activity if(playIntent==null) { playIntent=new Intent(this, MusicService.class); bindService(playIntent, conn,Context.BIND_AUTO_CREATE); startService(playIntent); } if(serviceConn) { //add listeners to all buttons in layout gd=new GestureDetectorCompat(this,this); gd.setOnDoubleTapListener(this); coverart=(ImageView) findViewById(R.id.imageView); b1 = (Button) findViewById(R.id.button1); b2 = (Button) findViewById(R.id.button2); b3=(Button)findViewById(R.id.button3); b4=(Button)findViewById(R.id.button4); tx1=(TextView)findViewById(R.id.textView1); tx2=(TextView)findViewById(R.id.textView2); seekbar=(SeekBar)findViewById(R.id.seekBar); seekbar.setClickable(false); b2.setEnabled(false); b3.setEnabled(false); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { musicService.playSongClicked(); } }); b3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { musicService.pause(); } }); b4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { musicService.jumpForward(); } }); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { musicService.jumpBackward(); } }); musicService.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer theMediaPlayer) { playSong(); } }); playSong(); getPlaylist(); } } //Runnable that update seekbar and duration label in layout private Runnable UpdateSongTime = new Runnable() { public void run() { double startTime = musicService.getStartTime(); tx1.setText(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes((long) startTime), TimeUnit.MILLISECONDS.toSeconds((long) startTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS. toMinutes((long) startTime))) ); seekbar.setProgress((int)startTime); myHandler.postDelayed(this, 100); } }; /** * Receiving song index from playlist view * and play the song * */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == 100){ Uri currentSong = Uri.parse(data.getExtras().getString("Uri")); musicService.songPlaying=currentSong; musicService.songID=data.getExtras().getString("songID"); musicService.playSong(); playSong(); } } //create layout according to song selected or playing in background void playSong() { if(musicService.songPlaying != null) { getCover(musicService.songPlaying); seekbar.setMax((int) musicService.finalTime); tx2.setText(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes((long) musicService.finalTime), TimeUnit.MILLISECONDS.toSeconds((long) musicService.finalTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) musicService.finalTime))) ); tx1.setText(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes((long) musicService.getStartTime()), TimeUnit.MILLISECONDS.toSeconds((long) musicService.getStartTime()) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) musicService.getStartTime())))); seekbar.setProgress((int)musicService.getStartTime()); myHandler.postDelayed(UpdateSongTime,100); b2.setEnabled(false); b3.setEnabled(true); } } //pause song void pause() { b2.setEnabled(true); b3.setEnabled(false); } //create songslist intent and show songs list activity protected void getPlaylist() { Intent i = new Intent(this, Playlist.class); startActivityForResult(i, 100); } //sets cover art in layout according to song selected protected void getCover(Uri currentSong) { android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(this, currentSong); byte [] data = mmr.getEmbeddedPicture(); //coverart is an Imageview object // convert the byte array to a bitmap if(data != null) { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); coverart.setImageBitmap(bitmap); //associated cover art in bitmap coverart.setAdjustViewBounds(true); } else { coverart.setImageResource(R.drawable.cover_art); //any default cover resourse folder coverart.setAdjustViewBounds(true); } } // rest below is ui and gesture no need for explanation protected void onSwipeRight() { } protected void onSwipeLeft() { musicService.getNextSong(); musicService.playSong(); } protected void onSwipeTop() { getPlaylist(); } protected void onSwipeBottom() { } @Override public boolean onTouchEvent(MotionEvent event) { gd.onTouchEvent(event); return super.onTouchEvent(event); } @Override public boolean onSingleTapConfirmed(MotionEvent e) { return true; } @Override public boolean onDoubleTap(MotionEvent e) { return true; } @Override public boolean onDoubleTapEvent(MotionEvent e) { return true; } @Override public boolean onDown(MotionEvent e) { return true; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return true; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Boolean result=false; try { float diffY = e2.getY() - e1.getY(); float diffX = e2.getX() - e1.getX(); if (Math.abs(diffX) > Math.abs(diffY)) { if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { if (diffX > 0) { onSwipeRight(); } else { onSwipeLeft(); } } result = true; } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if (diffY > 0) { onSwipeBottom(); } else { onSwipeTop(); } } result = true; } catch (Exception exception) { exception.printStackTrace(); } return result; } /*/prevents task from being destroyed @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { moveTaskToBack(true); return true; } return super.onKeyDown(keyCode, event); }*/ }
This is my manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.downloadsongsdownload.musiqa" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="18" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:enabled="true" > <activity android:name="com.downloadsongsdownload.musiqa.MainActivity" android:label="@string/app_name" android:launchMode="standard" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.downloadsongsdownload.musiqa.Playlist" ></activity> <service android:enabled="true" android:name="com.downloadsongsdownload.musiqa.MusicService" > </service> </application> </manifest>
Tim Moores
Saloon Keeper
Posts: 7585
176
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
That looks like the same code you posted earlier. Please post follow-up questions in the original topic. I'm closing this one.
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Main is not binding with service
Zoom image is not showing
Eclipse Android Programming/Buttons
Bizarre touch coordinates
No Activity found to handle Intent { act=com.application.activity.MainActivity }
More...