1

我有一个闹钟应用程序,可以在闹钟响起时播放音乐。这是我的第一个 Android 应用程序。当我在应用程序中时,我可以显示一个按钮并停止播放音乐。但是,我启用了通知,并希望在用户单击通知时停止播放音乐。我从通知返回到应用程序,但我无法停止音乐。当我从通知返回应用程序时,我显示的按钮不会显示,除非我按下返回键。如果我点击后退按钮,按钮就会出现。我已经查看了几个媒体播放器示例和通知示例,但似乎都没有我的问题。如果我能回到正确的位置,我知道如何停止音乐。通知管理器是否返回我可以检查的标志?我在这上面花了很多时间,也想不出一个好的解决方案。还,我的通知图标非常小。我怎样才能放大它?

这是我的代码:

    public void setAlarmPlaySong() {
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override public void onReceive( Context context, Intent _ )
        {   
            stopPlaying();
            // Set up for playing the song selected          
            switch(mPos) {
                case 0:
                    // Play song - 
                    mPlayer = MediaPlayer.create(SetAlarmActivity.this, R.raw.calmyourstorm);
                    mPlayer.start(); 
                break;
                case 1:
                    // Play song - 
                    //mPlayer = MediaPlayer.create(SetAlarmActivity.this, R.raw.gowiththeflow);
                    //mPlayer.start(); 
                break;
                case 2:
                    // Play song - 
                    mPlayer = MediaPlayer.create(SetAlarmActivity.this, R.raw.freeyourmind);
                    mPlayer.start(); 
                break;
                case 3:
                    // Play song - 
                break;  
                case 4:
                    // Play song - 
                break;  
            } // end switch         
            context.unregisterReceiver( this ); // this == BroadcastReceiver, not Activity
            showStopAlarmButton();
        } // end on Receive
    };  
        //
        //Intent intent = new Intent(getApplicationContext(), SetAlarmActivity.class);
        createNotification();        
        //Create alarm manager
        AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));

        this.registerReceiver( receiver, new IntentFilter("STOP_MUSIC") );
        PendingIntent sender = PendingIntent.getBroadcast( this, 0, new Intent("STOP_MUSIC"), 0 );
        //PendingIntent sender = PendingIntent.getBroadcast( this, 0, intent, 0 );

        //set the timer as a RTC Wakeup to alarm manager object
        manager.set( AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), sender );   
}

这里是 createNotification

public void createNotification() {
    // Create the notification that will display when the app is
    // closed.      

    Context ctx = getApplicationContext();

    // The intent that is triggered if the notification
    // is trigger
    Intent notificationIntent = new Intent(ctx, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx,
            YOUR_PI_REQ_CODE, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager nm = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);

    //Resources res = ctx.getResources();
    // Build notification
    Notification.Builder builder = new Notification.Builder(ctx);
    builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.beasleybackground2)
                //.setTicker(res.getString(R.string.your_ticker))
                //.setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Jazz Alarm")
                .setContentText("Good Morning!");
                //.setContentTitle(res.getString(R.string.your_notif_title))
                //.setContentText(res.getString(R.string.your_notif_text));
    Notification n = builder.build();

    // Hide the notification after its selected
    n.flags |= Notification.FLAG_AUTO_CANCEL;
    nm.notify(YOUR_NOTIF_ID, n);                   

}   // createNotification

这里是 showStopAlarmButton

        public void showStopAlarmButton() {
    final Button alarmOffButton = (Button) findViewById(R.id.btn_alarmOff);
    alarmOffButton.setVisibility(View.VISIBLE);
    alarmOffButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
            //When button is clicked stop the alarm and hide the button
            //when play is clicked show stop button and hide play button
            alarmOffButton.setVisibility(View.GONE);                
            // Cancel song
            stopPlaying();  
        }
    });     
}  
4

0 回答 0