我的应用程序将手机音量调到最大

时间:2014-01-10 14:29:23

标签: android volume

我的应用将手机音量调到最大。

package com.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {
private static final String number = "15555215556";

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    Object[] messages = (Object[]) bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];

    for (int i=0; i < messages.length; i++) {
        sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
    }
    for (SmsMessage msg : sms) {

        if (TextUtils.equals(msg.getOriginatingAddress(), number)) {
            Toast.makeText(context, "" + msg.getMessageBody(), Toast.LENGTH_SHORT).show();

            //AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

            MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.son1);
            mPlayer.setVolume(1.0f, 1.0f);
            mPlayer.start();
            abortBroadcast();
        }
    }
}

}

我在this.getSystemService上遇到错误,要求我创建getSystemService方法。 有人可以帮助我。 感谢。

3 个答案:

答案 0 :(得分:1)

getSystemService()方法派生自Context抽象类。 ServiceActivity类扩展了Context类,因此您可以从内部方法调用此方法。你试着从哪个班级来调用它?

答案 1 :(得分:0)

试试这个..

final AudioManager mAudioManager = (AudioManager) context.getSystemService(AudioManager.STREAM_MUSIC);

试试这个..

AudioManager mAudioManager;
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    Object[] messages = (Object[]) bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];

    for (int i=0; i < messages.length; i++) {
        sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
    }
    mAudioManager = (AudioManager) context.getSystemService(AudioManager.STREAM_MUSIC);
    for (SmsMessage msg : sms) {

        if (TextUtils.equals(msg.getOriginatingAddress(), number)) {
            Toast.makeText(context, "" + msg.getMessageBody(), Toast.LENGTH_SHORT).show();

            MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.son1);
            mPlayer.setVolume(1.0f, 1.0f);
            mPlayer.start();
            abortBroadcast();
        }
    }

或者

    @Override
    public void onReceive(final Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];

        for (int i=0; i < messages.length; i++) {
            sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
        }

        for (SmsMessage msg : sms) {

            if (TextUtils.equals(msg.getOriginatingAddress(), number)) {
                Toast.makeText(context, "" + msg.getMessageBody(), Toast.LENGTH_SHORT).show();
                AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.son1);
                mPlayer.setVolume(1.0f, 1.0f);
                mPlayer.start();
                abortBroadcast();
            }
        }

答案 2 :(得分:0)

我使用此代码:

mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);                 
player = new MediaPlayer();
player = MediaPlayer.create(MainActivity.this, R.raw.alert);

    if (player != null)
        {                                   
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
            mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_PLAY_SOUND);
            player.start();
        }

你说你正在使用BroadcastReceiver,这是我在BroadcastReceiver中获取上下文的方法,将方法更改为:

public class GPSStatusBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) { 

mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);                 
player = new MediaPlayer();
player = MediaPlayer.create(MainActivity.this, R.raw.alert);

    if (player != null)
        {                                   
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
            mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_PLAY_SOUND);
            player.start();
        }


}