【发布时间】:2011-03-08 07:01:12
【问题描述】:
我正在制作一个可以在后台运行的媒体播放器。我必须在这个服务中发送一个字符串 uri。 但我无法通过 bundle.Services 类从我的活动中发送 uri 值不支持 getIntent() 并显示错误。 我如何通过活动发送 uri 服务。 请帮忙
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 的值。Bundle 在此类中不起作用
【问题讨论】:
标签: android service android-activity android-intent