0

我正在尝试制作一个简单的警报触发器。当警报响起时,应该有一个简单的声音警报。但是警报没有响起,什么也没有发生。现在我不确定我在哪里弄错了 - 是时代吗?意图?接收器?

我在其中使用 AlarmManager 的类

package com.example.prva;


import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class Meni_Splash extends Activity{  
    Calendar c = Calendar.getInstance();
    private int hour = c.get(Calendar.HOUR_OF_DAY);
    private int minute = c.get(Calendar.MINUTE);
    int milivreme;
    int milipicker;
    long sveskupa;
    int hoursad;
    int minutesad;
    protected boolean timeactive = true;
    Handler h=new Handler();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);             
        h.post(new Runnable(){
            @Override
            public void run() {
                String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
                TextView text = (TextView) findViewById(R.id.textDate);
                text.setText(currentDateTimeString);
                h.postDelayed(this, 1000);
            }
        });     


        final TimePicker picker = (TimePicker) findViewById(R.id.timePicker);
        final TextView text = (TextView) findViewById(R.id.texttime);

        picker.setIs24HourView(true);       
        picker.setCurrentHour(hour);
        picker.setCurrentMinute(minute);

        Button btnt = (Button) findViewById(R.id.buttontime);       



        btnt.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                hoursad = picker.getCurrentHour();
                minutesad = picker.getCurrentMinute();
                milipicker = (((hoursad) * 60 * 60 * 1000) + ((minutesad) * 60 * 1000));
                text.setText(new StringBuilder().append(hoursad).append(":").append(minutesad).append( "Trenutno vrijeme u milisekundama : ").append(System.currentTimeMillis()).append(" vrijeme izabrano u milisekundama sve skupa ").append(milipicker));
            }
        });

        Button btnalarm = (Button) findViewById(R.id.btnalarm);
        btnalarm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                hoursad = picker.getCurrentHour();
                minutesad = picker.getCurrentMinute();
                milivreme = (((hour)* 60 * 60 * 1000)+ ((minute) * 60 * 1000));     
                milipicker = (((hoursad) * 60 * 60 * 1000) + ((minutesad) * 60 * 1000));
                sveskupa = milipicker - milivreme;

                Intent intent = new Intent(Meni_Splash.this, AlarmReceiver.class);
                PendingIntent pendingintent = PendingIntent.getActivity(Meni_Splash.this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);

                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + sveskupa, pendingintent);                      

            }
        });

        Button btnv = (Button) findViewById(R.id.buttonv);
        btnv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(Meni_Splash.this, button.class));              
            }
        });             
    }   
}

警报接收器类:

package com.example.prva;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;


public class AlarmReceiver extends Activity {
    protected MediaPlayer MPAlarm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        MPAlarm = MediaPlayer.create(this, R.raw.splash);
        MPAlarm.start();
    }




};
4

2 回答 2

1

您应该在清单上注册您的 AlarmReceiver:

<receiver android:process=":remote" android:name="com.yourpackage.AlarmReceiver" android:enabled="true" ></receiver>

不要忘记放置 android:process=":remote" 以使其工作。

于 2015-02-12T05:37:28.320 回答
0

看起来 AlarmManager set() 函数中存在问题。

警报管理器设置() =>

public void set(int类型,long triggerAtMillis,PendingIntent操作)

参数类型 ELAPSED_REALTIME、ELAPSED_REALTIME_WAKEUP、RTC 或 RTC_WAKEUP 之一。triggerAtMillis 警报应该响起的毫秒时间,使用适当的时钟(取决于警报类型)。operation 警报响起时执行的操作;通常来自 IntentSender.getBroadcast()。

请注意,操作 (PendingIntent) 将从 IntentSender.getBroadcast() 获取操作。希望它有帮助。

于 2013-01-17T23:57:22.860 回答