使用MediaPlayer强制关闭服务

时间:2010-08-01 00:10:36

标签: android

我有一个带有MediaPlayer的服务,它可以从互联网传输音频但是当我按下主页按钮并返回到主屏幕时,强制关闭。这很奇怪,因为它在模拟器中完美运行,有时它在我的手机上没有强制关闭(G1),当我刚打开它时。 这是我的代码:

    public class backgroundPlayer extends Service {

 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

 @Override
 public void onCreate() {

 }

 @Override
 public void onDestroy() {
  MyMainClass.myMediaPlayer.stop();
 }

 @Override
 public void onStart(Intent intent, int startid) {
  MyMainClass.myMediaPlayer.reset();

  try {  MyMainClass.myMediaPlayer.setDataSource(MyMainClass.channelUrl[player.selectedChannel]);
  } catch (IllegalArgumentException 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();
  }
  try {
   MyMainClass.myMediaPlayer.prepare();
  } catch (IllegalStateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  MyMainClass.myMediaPlayer.start();
 }
}

日志:

08-01 02:51:29.467: INFO/ActivityManager(123): Start proc com.android.settings for broadcast com.android.settings/.widget.SettingsAppWidgetProvider: pid=17820 uid=1000 gids={3003, 1015, 3002, 3001}
08-01 02:51:29.467: INFO/ActivityManager(123): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/.Launcher }
08-01 02:51:29.547: INFO/ActivityManager(123): Start proc android.process.acore for activity com.android.launcher/.Launcher: pid=17823 uid=10003 gids={1015, 3003}
08-01 02:51:30.287: INFO/ActivityManager(123): Process com.google.android.voicesearch (pid 17726) has died.
08-01 02:51:30.306: INFO/ActivityManager(123): Low Memory: No more background processes.
08-01 02:51:30.427: INFO/ActivityThread(17823): Publishing provider com.android.launcher.settings: com.android.launcher.LauncherProvider
08-01 02:51:30.457: INFO/ActivityThread(17823): Publishing provider user_dictionary: com.android.providers.userdictionary.UserDictionaryProvider
08-01 02:51:30.497: INFO/ActivityThread(17823): Publishing provider com.android.social: com.android.providers.contacts.SocialProvider
08-01 02:51:30.537: INFO/ActivityThread(17823): Publishing provider applications: com.android.providers.applications.ApplicationsProvider
08-01 02:51:30.757: INFO/ActivityThread(17823): Publishing provider contacts;com.android.contacts: com.android.providers.contacts.ContactsProvider2
08-01 02:51:31.457: INFO/ActivityManager(123): Process com.android.settings (pid 17820) has died.
08-01 02:51:31.467: INFO/ActivityManager(123): Low Memory: No more background processes.
08-01 02:51:31.667: DEBUG/dalvikvm(17823): GC_FOR_MALLOC freed 2871 objects / 200896 bytes in 116ms
08-01 02:51:31.687: INFO/ActivityThread(17823): Publishing provider call_log: com.android.providers.contacts.CallLogProvider
08-01 02:51:31.957: DEBUG/dalvikvm(17823): GC_EXTERNAL_ALLOC freed 967 objects / 49736 bytes in 110ms
08-01 02:51:33.867: DEBUG/dalvikvm(17823): GC_EXTERNAL_ALLOC freed 6359 objects / 416536 bytes in 154ms
08-01 02:51:34.666: DEBUG/dalvikvm(17823): GC_EXTERNAL_ALLOC freed 1253 objects / 75368 bytes in 306ms
08-01 02:51:35.297: INFO/ActivityManager(123): Displayed activity com.android.launcher/.Launcher: 83778 ms (total 5806 ms)
08-01 02:51:37.347: DEBUG/HomeLoaders(17823):   ----> items cloned, ready to refresh UI
08-01 02:51:38.237: DEBUG/Launcher(17823): about to setAppWidget for id=6, info=AppWidgetProviderInfo(provider=ComponentInfo{com.android.deskclock/com.android.alarmclock.AnalogAppWidgetProvider})
08-01 02:51:38.417: INFO/ActivityManager(123): Start proc com.android.deskclock for broadcast com.android.deskclock/com.android.alarmclock.AnalogAppWidgetProvider: pid=17862 uid=10011 gids={}
08-01 02:51:38.817: INFO/ActivityManager(123): Process com.ktix007.mira (pid 17708) has died.
08-01 02:51:38.817: INFO/WindowManager(123): WIN DEATH: Window{4407fd70 com.ktix007.mira/com.ktix007.mira.Mira paused=false}
08-01 02:51:38.827: WARN/ActivityManager(123): Scheduling restart of crashed service com.ktix007.mira/.bgPlayer in 5000ms
08-01 02:51:38.847: WARN/TimedEventQueue(96): Event 315 was not found in the queue, already cancelled?
08-01 02:51:38.857: INFO/WindowManager(123): WIN DEATH: Window{440a3ed0 com.ktix007.mira/com.ktix007.mira.player paused=false}
08-01 02:51:38.867: INFO/ActivityManager(123): Low Memory: No more background processes.
08-01 02:51:38.907: INFO/ActivityThread(17862): Publishing provider com.android.deskclock: com.android.deskclock.AlarmProvider
08-01 02:51:39.717: INFO/ActivityManager(123): Process com.android.deskclock (pid 17862) has died.
08-01 02:51:39.727: INFO/ActivityManager(123): Low Memory: No more background processes.
08-01 02:51:39.907: DEBUG/dalvikvm(17823): GC_FOR_MALLOC freed 7394 objects / 409864 bytes in 140ms
08-01 02:51:40.197: DEBUG/Launcher(17823): about to setAppWidget for id=2, info=AppWidgetProviderInfo(provider=ComponentInfo{com.google.android.googlequicksearchbox/com.google.android.googlequicksearchbox.SearchWidgetProvider})
08-01 02:51:40.247: INFO/ActivityManager(123): Start proc com.google.android.googlequicksearchbox for broadcast com.google.android.googlequicksearchbox/.SearchWidgetProvider: pid=17869 uid=10044 gids={3003}
08-01 02:51:41.017: INFO/ActivityThread(17869): Publishing provider com.google.android.googlequicksearchbox.google: com.google.android.googlequicksearchbox.google.GoogleSuggestionProvider
08-01 02:51:41.627: DEBUG/Launcher(17823): about to setAppWidget for id=7, info=AppWidgetProviderInfo(provider=ComponentInfo{com.android.settings/com.android.settings.widget.SettingsAppWidgetProvider})
08-01 02:51:41.667: INFO/ActivityManager(123): Start proc com.android.settings for broadcast com.android.settings/.widget.SettingsAppWidgetProvider: pid=17878 uid=1000 gids={3003, 1015, 3002, 3001}
08-01 02:51:42.747: INFO/ActivityManager(123): Process com.google.android.googlequicksearchbox (pid 17869) has died.
08-01 02:51:42.757: INFO/ActivityManager(123): Low Memory: No more background processes.
08-01 02:51:43.067: INFO/ActivityManager(123): Process com.google.android.apps.genie.geniewidget (pid 17590) has died.
08-01 02:51:43.077: WARN/ActivityManager(123): Scheduling restart of crashed service com.google.android.apps.genie.geniewidget/.GenieRefreshService in 10750ms
08-01 02:51:43.097: INFO/ActivityManager(123): Low Memory: No more background processes.
08-01 02:51:43.397: DEBUG/dalvikvm(17823): GC_FOR_MALLOC freed 8354 objects / 423984 bytes in 409ms
08-01 02:51:43.567: DEBUG/Launcher(17823): about to setAppWidget for id=8, info=AppWidgetProviderInfo(provider=ComponentInfo{com.google.android.apps.genie.geniewidget/com.google.android.apps.genie.geniewidget.miniwidget.MiniWidgetProvider})
08-01 02:51:43.667: INFO/ActivityManager(123): Start proc com.google.android.apps.genie.geniewidget for broadcast com.google.android.apps.genie.geniewidget/.miniwidget.MiniWidgetProvider: pid=17887 uid=10048 gids={3003, 1015}
08-01 02:51:43.887: INFO/ActivityManager(123): Start proc com.ktix007.mira for service com.ktix007.mira/.bgPlayer: pid=17892 uid=10053 gids={3003}
08-01 02:51:44.576: DEBUG/AndroidRuntime(17892): Shutting down VM
08-01 02:51:44.576: WARN/dalvikvm(17892): threadid=1: thread exiting with uncaught exception (group=0x400207e8)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892): FATAL EXCEPTION: main
08-01 02:51:44.677: ERROR/AndroidRuntime(17892): java.lang.RuntimeException: Unable to start service com.ktix007.mira.bgPlayer@43c9e050 with null: java.lang.NullPointerException
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at android.app.ActivityThread.access$3600(ActivityThread.java:125)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at android.os.Looper.loop(Looper.java:123)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at java.lang.reflect.Method.invoke(Method.java:521)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at dalvik.system.NativeStart.main(Native Method)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892): Caused by: java.lang.NullPointerException
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at com.ktix007.mira.bgPlayer.onStart(bgPlayer.java:35)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at android.app.Service.onStartCommand(Service.java:420)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
08-01 02:51:44.677: ERROR/AndroidRuntime(17892):     ... 10 more
08-01 02:51:45.137: DEBUG/Launcher(17823): about to setAppWidget for id=10, info=AppWidgetProviderInfo(provider=ComponentInfo{com.android.music/com.android.music.MediaAppWidgetProvider})
08-01 02:51:45.197: INFO/ActivityManager(123): Process com.android.settings (pid 17878) has died.
08-01 02:51:45.197: INFO/ActivityManager(123): Low Memory: No more background processes.
08-01 02:51:45.267: INFO/ActivityThread(17887): Publishing provider com.google.android.apps.genie.geniewidget.weather: com.google.android.apps.genie.geniewidget.providers.WeatherProvider
08-01 02:51:45.417: INFO/ActivityThread(17887): Publishing provider com.google.android.apps.genie.geniewidget.newsimage: com.google.android.apps.genie.geniewidget.providers.NewsImageProvider
08-01 02:51:46.197: WARN/Genie(17887): Cache file not found: java.io.FileNotFoundException: /data/data/com.google.android.apps.genie.geniewidget/files/genie_iconcache (No such file or directory)
08-01 02:51:46.487: DEBUG/Launcher(17823): about to setAppWidget for id=11, info=AppWidgetProviderInfo(provider=ComponentInfo{com.google.android.youtube/com.google.android.youtube.widget.WidgetProvider})
08-01 02:51:46.896: DEBUG/dalvikvm(17887): GC_FOR_MALLOC freed 9118 objects / 346184 bytes in 247ms
08-01 02:51:47.437: INFO/ActivityManager(123): Start proc com.android.music for broadcast com.android.music/.MediaAppWidgetProvider: pid=17907 uid=10020 gids={3003, 1015}
08-01 02:51:47.517: INFO/ActivityManager(123): Process com.google.android.apps.genie.geniewidget (pid 17887) has died.
08-01 02:51:47.517: INFO/ActivityManager(123): Low Memory: No more background processes.
08-01 02:51:48.137: INFO/dalvikvm(17823): Jit: resizing JitTable from 512 to 1024
08-01 02:51:48.277: INFO/ActivityManager(123): Start proc com.google.android.youtube for broadcast com.google.android.youtube/.widget.WidgetProvider: pid=17914 uid=10023 gids={3003}
08-01 02:51:48.587: INFO/ActivityManager(123): Process com.android.music (pid 17907) has died.
08-01 02:51:48.587: INFO/ActivityManager(123): Low Memory: No more background processes.
08-01 02:51:48.647: INFO/ActivityThread(17914): Publishing provider com.google.android.youtube.SuggestionProvider: com.google.android.youtube.suggest.SuggestionProvider
08-01 02:51:49.027: INFO/YouTube(17914): Distribution channel:mvapp-android-google
08-01 02:51:49.827: INFO/ActivityManager(123): Process com.google.android.youtube (pid 17914) has died.
08-01 02:51:49.827: INFO/ActivityManager(123): Low Memory: No more background processes.

2 个答案:

答案 0 :(得分:0)

我不是专家,但我会尝试提供答案,因为我一直在处理同样的事情。

好的,看看是否有任何帮助:

1)摆脱onStart()我甚至不认为已经使用了?

2)将onStart()中的所有代码放入onCreate()。不要忘记super.onCreate();作为onCreate()方法的第一行。

3)作为onDestroy()方法的第一行,您需要super.onDestroy();

4)停止后,在MediaPlayer.release();方法中也执行onDestroy()

答案 1 :(得分:0)

当内存不足时,Android会释放所有静态变量。它使静态变量为null。 这可能是NPE的来源(NullPointerException)。