2011-09-19 45 views
1

我正嘗試通過應用程序的幫助從互聯網上下載歌曲。我編碼它顯示通知時,下載完成,但問題是我不知道當通知單擊時該怎麼做。我希望它在點擊通知時播放歌曲。如何在用戶按下通知後播放音頻文件?

String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 
     int icon = R.drawable.m;  // icon from resources 
     CharSequence tickerText = "Song Downloaded";    // ticker-text 
     long when = System.currentTimeMillis();   // notification time 
     Context context = getApplicationContext();  // application Context 
     CharSequence contentTitle = name; // expanded message title 
     CharSequence contentText = "Your song "+name +" has been Song Downloaded";  // expanded message text 

     Intent notificationIntent = new Intent(this, notif.class); 
     notificationIntent.putExtra("song", name); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

     // the next two lines initialize the Notification, using the configurations above 
     Notification notification = new Notification(icon, tickerText, when); 
     notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); 
     notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL; 
     notification.ledARGB = 0xff0000ff; 
     notification.ledOnMS = 500; 
     notification.ledOffMS = 3000; 
     notification.defaults |= Notification.DEFAULT_LIGHTS; 
     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
     final int HELLO_ID = 1; 

     mNotificationManager.notify(HELLO_ID, notification); 

這清楚地表明意圖開始notif.class,但我不知道在notif.class我想這做,但是當我在點擊通知符仍犯規顯示任何東西。

class notif extends Activity 
    { 
MediaPlayer abc=new MediaPlayer(); 
public void onCreate(Bundle icicle){ 
File SDCardRoot = Environment.getExternalStorageDirectory(); 
Intent intent = getIntent();   
Bundle bundle = intent.getExtras();     
String name = bundle.getString("song"); 
try { 
    abc.setDataSource(SDCardRoot.getAbsolutePath() + "/Music/"+name+".mp3"); 
    abc.prepare(); 
} catch (IllegalArgumentException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IllegalStateException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

    if(name!="") 
{ abc.start(); } 
    } 
    } 

回答

1

試試這個:

@Override 
    public void onNewIntent(Intent intent) { 

      Bundle extras = intent.getExtras(); 
      if (extras!=null) { 
         ... 
      } 


    } 
+0

哎lacas ..thanks爲...我想要一個小的幫助......我想知道其中u要我把其中的類方法?在此先感謝:) –

+1

把它放在你的活動 – lacas

+0

**非常感謝** –