开发者

Using a variable in one class in my service

This is a part of my main class:

  @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.printSta开发者_Python百科ckTrace();
            }
            filename = songURL.getFile();
                    startService(new Intent(mainmenu.this, MyService.class));

Now this should get the name of the song being played, but I have a service that starts a notification when the song is playing and I want it to display the name of the file, so how can I pass this variable to my service class?

Here is my service class, I want to display it under contentText where it says "Now Playing..."

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);      
}       
}


I'm not sure if it works with a Service, but I think you can create a Bundle with the Intent and then get the data from that. Try this in your main class:

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);

And then do this in your Service class, to get the data:

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


You can easily add whatever information you want in the Intent you use to start the service:

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

Then in your service you can retreive the information with:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜