android - android服务未定义布局

标签 android audio service wakelock

我已使该服务播放声音,但与我创建的内容可能会相差甚远,因为我所遵循的是一个简单的教程,该教程甚至与我尝试做的事情都不尽相同。在任何情况下,我都将其设置在我的主要 Activity 中,即单击该按钮时,它将启动该服务。该服务在构建中有错误,我无法运行它。在onCreate();中它在SetContentView和FindViewById行上有错误。我想知道为什么会这样,如果这甚至是使用服务播放声音的正确方法,那么屏幕也不会超时。

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Shipservice extends Service implements View.OnClickListener {
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button2;
public Spinner spinner2;
public PowerManager.WakeLock wl;  
static final String TAG=Shipservice.class.getSimpleName();

@Override
public IBinder onBind(Intent intent) {
    return null;
    // TODO Auto-generated method stub

}

public void onCreate(Bundle savedInstanceState) {
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "wake");

    super.onCreate();
    wl.acquire();
    setContentView(R.layout.ship);
    button2 = (Button) findViewById(R.id.btn2);
    button2.setOnClickListener(this);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
            android.R.layout.simple_spinner_item, TIME_IN_MINUTES);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);
    Log.d(TAG, "onCreate'd");
}
// Play the sound and start the timer
private void playSound(int resourceId) {
    // Cleanup any previous sound files
    cleanup();
    // Create a new media player instance and start it

    mediaPlayer = MediaPlayer.create(this, resourceId);
    mediaPlayer.start();
    // Create the timer to stop the sound after x number of milliseconds
    int selectedTime = TIME_IN_MINUTES[spinner2.getSelectedItemPosition()];
    handler.postDelayed(runnable, selectedTime * 60 * 1000);
}

// Handle button callback
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn2:
        playSound(R.raw.ocean_ship);
        break;
    }
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    cleanup();
    super.onDestroy();
    Log.d(TAG, "onDestroy'd");
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Log.d(TAG, "onStart'd");
}

public void cleanup() {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;

    }
    // Cancel any previously running tasks
    handler.removeCallbacks(runnable);
}

// Runnable task used by the handler to stop the sound
public Runnable runnable = new Runnable() {
    public void run() {
        cleanup();
    }
};


protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release();
}

}

最佳答案

服务没有用户界面!创建一个Activity与用户进行交互,并使其通过startService(intent)将请求转发到服务

针对评论中的问题:

创建一个从IntentService派生的新类。
在该类中,实现onHandleIntent(Intent intent)方法。
在 list 文件中添加新服务的声明。

在您的应用程序中:
当用户采取应引起声音播放的 Action 时:
创建一个明确引用新服务的Intent。
向意图中添加额外信息以告知服务该怎么做
(您也可以为此使用Intent的Action字段)
调用startService(intent)方法将意图发送给服务。

哪些功能应移至服务,由您自己决定。基本上,任何花费时间超过几毫秒的事情都应该投入使用。

有很多可用的示例。像IntentService这样的Google会播放声音以找到它们。

关于android - android服务未定义布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391747/

相关文章:

java - 当设备处于 sleep 状态时在后台运行广播接收器?

java - 如何在运行时以智能方式在多个 OSGi 服务中进行选择?

java - 在 Activity1 fragment 上使用从 Activity2 接口(interface)检索的数据时出现问题

android:textColor 在支持库 23.2.1 更新后不工作

android - 具有不同上下文的 PendingIntent FLAG_NO_CREATE

java - 使用java创建Windows服务

android - 从 Android 向 node.js 服务器发送带有 JSON 的 POST 请求

java - 剪辑有时无法播放

c# - 如何在 Windows 窗体应用程序中依次播放两种声音?

jquery - jQuery keydown和mouseDown函数?