在我的服务中使用一个类中的变量

时间:2011-09-07 21:17:54

标签: android android-webview android-service

这是我主要课程的一部分:

  @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("http://xxxxxx.com/songs2/Music%20Promotion/Stream/")) {                             
            try {
                songURL = new URL(url);
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            filename = songURL.getFile();
                    startService(new Intent(mainmenu.this, MyService.class));

现在这应该得到正在播放的歌曲的名称,但我有一个服务,当歌曲播放时开始通知,我希望它显示文件的名称,所以我怎么能把这个变量传递给我服务类?

这是我的服务类,我想在contentText下显示它,其中显示“正在播放...”

public class MyService extends Service {

private static final int HELLO_ID = 1;
private static final String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager;


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

@Override
public void onCreate() {

}

@Override
public void onStart(Intent intent, int startid) {

    Context context2 = getApplicationContext();
    CharSequence text = "Buffering...";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context2, text, duration);
    toast.show();

    mNotificationManager = (NotificationManager) getSystemService(ns);

    int icon = R.drawable.notification_icon;
    CharSequence tickerText = "Now playing...";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);       
    Context context = getApplicationContext();
    CharSequence contentTitle = "Music Promotion";
    CharSequence contentText = "Now Playing...";
    Intent notificationIntent = new Intent(this, mainmenu.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);             
    mNotificationManager.notify(HELLO_ID, notification);        
}
@Override
public void onDestroy() {
    mNotificationManager.cancel(HELLO_ID);      
}       
}

2 个答案:

答案 0 :(得分:1)

我不确定它是否适用于Service,但我认为您可以使用Bundle创建Intent,然后从中获取数据。在你的主要课程中试试这个:

Bundle bundle = new Bundle();
bundle.putString("variablename", "some data"); // Basically just a name and your data

// Create a new Intent with the Bundle
Intent intent = new Intent();
intent.setClass(mainmenu.this, MyService.class);
intent.putExtras(bundle);
startService(intent);

然后在Service课程中执行此操作以获取数据:

Bundle bundle = this.getIntent().getExtras();
String variable = bundle.getString("variablename"); // Retrieve your data using the name

答案 1 :(得分:0)

您可以在用于启动服务的Intent中轻松添加所需的任何信息:

Intent i = new Intent(mainmenu.this, MyService.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("songName", "THIS IS THE SONGS NAME");

然后在您的服务中,您可以通过以下方式检索信息:

Bundle extras = this.getIntent().getExtras(); 
String songName = null;

if (extras != null) {
    songName = extras.getString("songName");
}