如何通过在服务价值? [英] How to pass a value in Services?
问题描述
我想提出一个媒体播放器,谁能够在后台运行。我必须在这个服务发送一个字符串URI。 但我无法从我的活动通过bundle.Services类发送URI值亘古不支持getIntent()并显示错误。 我如何通过活动发送URI的服务。 请大家帮忙
I am making a media player ,who can run in background. i have to send a string uri in this service. but i cant send the uri value from my activity through bundle.Services class doesnot support getIntent() and show error. how can i send uri in service through activity. please help
package com.abc.activity;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
String uri,url;
Bundle b;
AudioPlayer2 ap;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
Uri path=Uri.parse("/mnt/sdcard/download/abc.mp3");
player = MediaPlayer.create(this,path );
player.setLooping(false); // Set looping
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
//Bundle extras = getIntent().getExtras();
player.start();
}
public void getValue(String uri)
{
url=uri;
}
}
这正与静态的URL,我在这个例子中使用。请建议我能取URI的价值,从我的活动class.Bundle不是工作在这个类
This is working with static url which i use in this example. please suggest what can i fetch uri's value from my activity class.Bundle is not working in this class
推荐答案
您应该使用IntentService的 http://developer.android.com/reference/android/app/IntentService.html 。
You should use IntentService http://developer.android.com/reference/android/app/IntentService.html.
实施 onHandleIntent(意向意图)
将在每次启动的服务时间称为 startService(意向)
。你可以通过你的数据通过这个意图的服务。
Implement onHandleIntent(Intent intent)
which will be called each time you start service with the startService(intent)
. You can pass your data to service through this intent.
这篇关于如何通过在服务价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!