java - 关闭铃声管理器歌曲 android

标签 java android eclipse

我尝试了很多解决方案,例如 -

RingtoneManager.stopPreviousRingtone();

但它带有错误-
无法从 RingtoneManager 类型中静态引用非静态方法 stopPreviousRingtone()

MyAlarmService.java

package com.example.getbettersoon;

import android.app.Service;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
import android.widget.Toast;

public class MyAlarmService extends Service {

    static Ringtone ringtone;

    @Override
    public void onDestroy() {
        super.onDestroy();
        //Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
        //RingtoneManager.stopPreviousRingtone();
        ringtone.stop();   // THIS DOES NOT STOP THE RINGTONE
    }

    @SuppressWarnings("deprecation")
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
         Uri alarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
         ringtone = RingtoneManager.getRingtone(getApplicationContext(), alarm);
         ringtone.play();
    }


}

Alarm.java

package com.example.getbettersoon;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.Ringtone;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Alarm extends Activity {

    String gethours;
    int myNum;
    private PendingIntent pendingIntent;


            public void SetAlarm(View arg0) {

                Intent myIntent = new Intent(Alarm.this, MyAlarmService.class);
                pendingIntent = PendingIntent.getService(Alarm.this, 0, myIntent, 0);
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.SECOND, 10);          // test

                //TextView tv = (TextView) findViewById(R.id.bthrs);
                //tv.setText(myNum);

                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
                Toast.makeText(Alarm.this, "Start Alarm", Toast.LENGTH_LONG).show();

            };


            public void OffAlarm(View arg0) {
                Intent myIntent = new Intent(Alarm.this, MyAlarmService.class);
                pendingIntent = PendingIntent.getService(Alarm.this, 0, myIntent, 0);
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                alarmManager.cancel(pendingIntent);

                // Tell the user about what we did.
                Toast.makeText(Alarm.this, "Cancel!", Toast.LENGTH_LONG).show();

            };

    }

当我按下按钮时,铃声会在 10 秒后开始,但当我按下停止按钮时,音乐不会停止。谁能帮我解决一下吗?

最佳答案

我假设您依靠停止服务来停止铃声播放。该行:

alarmManager.cancel(pendingIntent);

仅删除 AlarmManager 将来部署的服务,并且不会停止 Activity 服务。

要停止正在运行的服务,您需要使用Context.stopService()

例如:

public void OffAlarm(View arg0){
    Intent myIntent = new Intent(Alarm.this, MyAlarmService.class);
    pendingIntent = PendingIntent.getService(Alarm.this, 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);

    // Stops an existing running service

    stopService(myIntent);

    // Tell the user about what we did.
    Toast.makeText(Alarm.this, "Cancel!", Toast.LENGTH_LONG).show();
}

只有这样,Android系统才会调用onDestroy()方法,并且您的铃声就会停止。

关于java - 关闭铃声管理器歌曲 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386016/

相关文章:

java - Eclipse 上的 Alfresco Mobile Android 错误

java - 同步ArrayList作为静态方法参数

java - 使用蓝牙适配器将字符串从 PC 发送到 Android 应用程序

eclipse - 从命令行打开特定的 Eclipse 项目

android:使用Intent.ACTION_BOOT_COMPLETED还是...?

android - T-Mobile Galaxy S 2 在市场上看不到我的应用程序

java - 可以将 JAXX 与 Eclipse 一起使用吗?

java - 如何在遍历所有按钮时解决陈旧元素?

java - 在 Jboss 上使用 CMS 和 Luna Hsm 解密 p7m

Java 8 将对象添加到列表的有效方法