0

我正在尝试创建我的第一个 android 服务并且遇到了一些问题。我已经看过了,不知道我做错了什么。我的目标是创建一项服务,在打开免提电话时最大化我的音量。

package com.example;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by IntelliJ IDEA.
 * User: Matthew
 * Date: 12/17/10
 * Time: 9:36 AM
 * To change this template use File | Settings | File Templates.
 */
public class MyService extends Service {

    private Timer timer = new Timer();//timer constructor
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate()
    {

        Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
        super.onCreate();
        startService();
    }

    public void startService()
    {
         Toast.makeText(this, "Service started...", Toast.LENGTH_LONG).show();

         timer.scheduleAtFixedRate(new TimerTask()
         {
              AudioManager am;
              public boolean speakerPhone = false;
              @Override
              public void run()
              {
              //I am not sure what should go here...
              //I want the volume to go to max volume when I turn the speaker phone on

                   /*
                   //if(am.isSpeakerphoneOn())
                   {
                        am.adjustStreamVolume(AudioManager.STREAM_VOICE_CALL,                   
                              AudioManager.ADJUST_RAISE, 2);
                   }
                   */
              }
         },0,1000);
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        stopService();
    }
    public void stopService()
    {
        Toast.makeText(this, "Service stopped...", Toast.LENGTH_LONG).show();
        if(timer != null)
        {
            timer.cancel();
        }
    }
}
4

1 回答 1

0

首先,您可能想提供更多信息。比如输出等等。它编译了吗?当您运行它时,该服务是否显示在进程列表中?

二是拆散。制作一个启动 hello world 的服务。制作一个改变音量的应用程序。编写大量小的 Android 测试用例是探索 API 的最佳方式。

第三,对 IBinder 的覆盖看起来非常奇怪。花点时间浏览一下 Alarm 应用程序的 API Demo 代码,特别是 apis/app/AlarmService_Service.java。

坚持下去。

查尔斯

于 2010-12-18T03:21:11.933 回答