1

I'm very new to android. I want to create an application that turns off all sounds at the selected time. I created a service with some code and in Eclipse there's no errors, but when I press the button nothing happens. I can see in Application Manager that my program and the service SilentHours are running. Here's my code:

MainActivity.java

package com.example.silencecontrol;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    public final static String EXTRA_NAME = "sending silentHour value to service SilenceHours";

    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
   }

    public void setSilenceHours(View view) {
        EditText editText1 = (EditText)findViewById(R.id.editText1);
        TextView textView1 = (TextView)findViewById(R.id.textView1);
        if(editText1.length() > 0) {
            Intent intent = new Intent(this, SilentHours.class);
            String editText1String = editText1.getText().toString();
            int silentHour = Integer.parseInt(editText1String);
            intent.putExtra(EXTRA_NAME, silentHour);
            this.startService(intent);
        } else {
            textView1.setText("Please enter the silence hour. ");
        }
   }

}

SilentHours.java

package com.example.silencecontrol;

import java.util.Calendar;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;

public class SilentHours extends Service {
    public SilentHours() {
    }

    protected void onHandleIntent(Intent intent) {
        int silentHour = Integer.parseInt(intent.getStringExtra(MainActivity.EXTRA_NAME));
        Calendar calendar = Calendar.getInstance();
        int currentTime = calendar.get(Calendar.HOUR_OF_DAY);
        if(currentTime >= silentHour) {
            AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

And by the way I can't @Override the onHandleIntent method. If I put the @Override annotation I get error:

The method onHandleIntent(Intent) of type SilentHours must override or implement a supertype method.

Is that annotation necessary?

4

1 回答 1

0

您正在寻找的方法是命名的onStartCommand,而不是onHandleIntent

不,没有必要将此注释添加到重写方法,这只是一个非常好的做法,因为您可以确保您尊重超类的签名。

让您在编码时获得 IDE 帮助。只需键入“on”,然后要求完成以查看可以覆盖的方法。

于 2013-10-15T18:03:25.043 回答