Android 如何在用户按下通知后播放音频文件?

Android 如何在用户按下通知后播放音频文件?,android,android-intent,android-notifications,Android,Android Intent,Android Notifications,我正试图通过一个应用程序从互联网上下载一首歌曲。我将其编码为在下载完成时显示通知,但问题是单击通知时我不知道该怎么办。我希望它在单击通知时播放歌曲 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); int icon = R.drawable.m;

我正试图通过一个应用程序从互联网上下载一首歌曲。我将其编码为在下载完成时显示通知,但问题是单击通知时我不知道该怎么办。我希望它在单击通知时播放歌曲

 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中该做什么我尝试了这个,但当我点击notif时,它仍然没有显示任何内容

 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(); }
  }
   }
试试这个:

   @Override
   public void onNewIntent(Intent intent)  {

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


   }
试试这个:

   @Override
   public void onNewIntent(Intent intent)  {

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


   }

嘿,拉卡斯…谢谢你…我需要一点帮助…我想知道你想让我把这个方法放在哪一个类中?提前感谢:)嘿,拉卡斯……谢谢你……我需要一点帮助……我想知道你想让我把那个方法放在哪一个类中?提前感谢:)