- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我单击播放按钮播放歌曲并将歌曲 mp3 与其 bundle 在一起并在 onStartCommand 中接收时,我就开始了我的服务。问题是当我启动服务的 Activity 结束时,我的服务再次调用 onStartCommand。当它去接收那个包时,它不在那里,因为这次 Activity 没有启动它。正因为如此,我在准备我的媒体播放器时遇到了 illegalstateexception。我不绑定(bind)我的服务。
为什么在我的 Activity 结束时调用 onStartCommand?
启动服务:
Intent i = new Intent(SongList.this,MyService.class);
i.putExtra("songURL", user.songurlz);
i.putExtra("songNAME", songplay_name);
startService(i);
服务类:
public class MyService extends Service {
static MediaPlayer mediaPlayer;
static int pauseplay;
static NotificationManager notificationManagerPlay;
static CharSequence tickerText;
static Notification notification4;//Notification variable(for song playing)
TelephonyManager telephonyManager;
PhoneStateListener listener;
static Notification notification;
static NotificationManager mNotificationManager;
String songurl;
String songname;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
pauseplay = 1;
MainMenu.serviceRunning = 1;
telephonyManager = (TelephonyManager) getSystemService(
Context.TELEPHONY_SERVICE);
// Create a new PhoneStateListener
listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_RINGING:
if (mediaPlayer.isPlaying() == true && mediaPlayer != null){
pauseSong();
}
break;
}
}
};
// Register the listener wit the telephony manager
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (intent != null) {
Bundle bundle = intent.getExtras();
//Retrieve your data using the name
songurl = bundle.getString("songURL");
Bundle bundle2 = intent.getExtras();
//Retrieve your data using the name
songname = bundle2.getString("songNAME");
}
Toast toast = Toast.makeText(getApplicationContext(), "Now Playing: "
+ songname, Toast.LENGTH_LONG);
toast.show();
// configure the intent
Intent playIntent = new Intent(MyService.this, SongList.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(
MyService.this, 0, playIntent, 0);
notification = new Notification(R.drawable.playicon, "Buffering...",
System.currentTimeMillis());
notification.contentView = new RemoteViews(getPackageName(),
R.layout.custom_notification2);
notification.contentIntent = pendingIntent;
if (SongList.bitmap != null) {
notification.contentView.setImageViewBitmap(R.id.notifimage,
SongList.bitmap);
} else {
notification.contentView.setImageViewResource(R.id.notifimage,
R.drawable.icon);
}
notification.contentView
.setTextViewText(R.id.notiftitle, "Now Playing");
notification.contentView.setTextViewText(R.id.notiftext, songname);
notification.flags = notification.flags
| Notification.FLAG_ONGOING_EVENT;
mNotificationManager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(4, notification);
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(songurl);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// register an error listener via MediaPlayer's setOnErrorListener
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e("MEDIAPLAYER ERRORS", "what: " + what + " extra: "
+ extra);
mediaPlayer.reset();
mNotificationManager.cancel(4);
return false;
}
});
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
stopSelf();
}
});
return START_STICKY;
}
@Override
public void onDestroy() {
MainMenu.serviceRunning = 0;
mNotificationManager.cancel(4);
}
public static void pauseSong() {
if (mediaPlayer.isPlaying() == true) {
mediaPlayer.pause();
mNotificationManager.cancel(4);
pauseplay = 0;
}
}
public static void playSong() {
if (pauseplay == 0) {
mNotificationManager.notify(4, notification);
mediaPlayer.start();
pauseplay = 1;
}
}
}
最佳答案
Why does the onStartCommand get called when my activity ends?
当您的“Activity 结束”(无论是什么意思)时您正在调用 startService()
,或者由于您使用 START_STICKY
而导致 Android 出于某种原因重新启动您的服务>.
就个人而言,对于音乐播放器,我认为 START_NOT_STICKY
是正确的答案。如果您的服务因任何原因停止,用户应负责重新启动它。
关于android - Activity 结束后调用服务 onStartCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9399701/
我在 Android 上开发了一个 GPS 跟踪应用程序。我使用 AlarmManager 每 5 分钟唤醒一次设备。我开发了一个日志文件,显示该应用程序运行良好,直到我收到 public int o
我有一个简单的服务: public class TestService extends Service { final String LOG_TAG = "myLogs"; public void o
当我调用 this.startService(new Intent(this, Some_Service.class)); 我知道 onCreate() 被调用,然后是 onStartCommand(
我在使用实现 onStartCommand() 的服务时有点困惑。 在某些示例中,onStartcommand() 方法已实现,但在某些地方未实现。对于我需要使用此方法的原因,您能否澄清我的疑问。 最
我有一个测试项目,我的简单测试用例扩展了AndroidTestCase类: public class MyTest extends AndroidTestCase{ private Conte
我有一项服务,只要用户使用它,我就会尝试持续运行。问题是该服务使用了大量内存;我猜是因为它会通知用户并不断显示图像。所以我将大部分代码放在另一个 Activity 中。服务只是调用 Activity
我有一个后台服务,我可以使用应用程序菜单启动或停止该服务。该服务也可以在模拟器设备启动时启动。但奇怪的是,我只在设备启动时看到 toast 消息,而 jobThread 没有启动......即使我从菜
我想知道如果两个不同的线程同时调用服务A上的startService,服务是否执行onStartCommand() 两者分开还是一个取消另一个? 我想了解 Android 中的服务。 最佳答案 Ser
我必须确保我的 Service 会运行,直到我显式调用 stopService()。所以我应该从 onStartCommand() 返回 START_STICKY 标志。 我还希望万一我的服务因为内存
我注意到 Service.START_STICKY 不工作,当我仔细观察时,我看到 onCreate() 正在运行但没有调用 onStartCommand。 有什么想法吗? @Override
当我单击播放按钮播放歌曲并将歌曲 mp3 与其 bundle 在一起并在 onStartCommand 中接收时,我就开始了我的服务。问题是当我启动服务的 Activity 结束时,我的服务再次调用
我正在尝试创建一个将在后台处理文件 I/O 的服务。更新数据的 Activity 将绑定(bind)到服务并调用服务的方法来执行 I/O。我正在使用 Android 文档作为指导。 但是,我的服务似乎
我一直在查看文档,有时 onStartCommand() 返回 START_NOT_STICKY,有时返回以下内容: @Override public int onStartCommand(Inten
我有一个服务,我需要从服务 onStartCommand 访问上下文。 我怎么能这样。我是 Android 新手。 最佳答案 可以使用ServiceName.this获取Context,Service
我想从我的 Activity 类(class)中启动一项服务。问题是,onStartCommand 永远不会被调用。我在那里放置了断点,但它从未被击中。我的例子非常简单,我不明白为什么它不起作用。 M
我正在尝试在 android 中创建一个简单的服务。 我检查了“设置”->“应用程序”选项卡,可以看到该服务正在运行。不幸的是 OnStartCommand 函数没有被执行。我什至看不到 toast
我有一个运行两个独立服务的应用程序。其中一个运行平稳,另一个大部分时间都没有问题,但抛出一个 NullPointerException在 onStartCommand() 内在 4.1 和 4.4.2
我目前正在从 Service 类扩展我的自定义服务实现。作为 Android-O 迁移的一部分,我想使用 JobIntentService。 目前我所有的逻辑都在服务的 onStartCommand
我有以下代码 @Override public int onStartCommand(Intent intent, int flags, int startId) { if(intent !=
在 Android 文档中,服务的“onStartCommand()”有一个作为 param 给出的 Intent ,根据文档: “提供给 startService(Intent) 的 Intent,
我是一名优秀的程序员,十分优秀!