Android的报警器不工作 [英] Android alarm not working

查看:132
本文介绍了Android的报警器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力奋斗着这个好几个小时。我也查了一些文档和几个主题。我发现这个code在两个主题,男生们都表示,code的正常使用,但不能在我的电脑。第一个敬酒出现,但第二个从来没有。什么是错的?

I've been struggling with this for hours. I've also checked the documentation and several topics. I found this code in two topics, both guys said the code was working perfectly, but not on my computer. The first Toast appears, but the second one never. What is wrong?

public class HelloAndroid2 extends Activity {  


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);   

    Intent intent = new Intent(this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

}

 public final class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

    }
 }

}

既然不能添加XML code在这里,这里的链接打印屏幕: 链接文本

Since i cannot add the xml code here, here's the link to a print screen: link text

推荐答案

其实你不需要,因为你使用的类 AlarmReceiver.class 的意图指定动作。

Actually you dont need to specify the action since you use the class AlarmReceiver.class in the intent.

在你的的Andr​​oidManifest.xml ,确保您有&LT内的接收器的定义;用途> 标签,是这样的:

In your AndroidManifest.xml, make sure you have a receiver definition within the <application> tags, something like:

&LT;接收器的Andr​​oid版本:NAME =AlarmReceiver&GT;

编辑: 确定有2种方式来使用你的广播接收器。

Ok there are 2 ways to use your broadcast receiver.

1)从C您所提供的$ C $, AlarmReceiver.java 这将会包含:

1) From the code you have provided, AlarmReceiver.java that will contains:

public final class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

    }
 }

HelloAndroid2.java

public class HelloAndroid2 extends Activity {  


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);   

    Intent intent = new Intent(this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

}

}

就像这样,你可以设置你的广播接收器与的Andr​​oidManifest.xml 和标签的工作&LT;接收器...&GT;

2)第二种方法。通过这种方式,你可以只使用1档 HelloWorld2.java

2)2nd way. With this way, you can use just 1 file HelloWorld2.java:

在你的活动,创建广播接收器并将其注册。

In your activity, create your broadcast receiver and register it.

public class HelloWorld2 extends Activity {
    private SharedPreferences prefs;
    private String mName;


    BroadcastReceiver alarmReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();          
        }
    };


    public static final String ACTION_NAME = "com.helloworld.MYACTION";
    private IntentFilter myFilter = new IntentFilter(ACTION_NAME);


    @Override
    protected void onPause() {
        unregisterReceiver(alarmReceiver);
        super.onPause();
    }

    @Override
    protected void onResume() {
        registerReceiver(alarmReceiver, myFilter);
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        registerReceiver(alarmReceiver, myFilter);

        Intent intent = new Intent(ACTION_NAME);        

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();


    }

这篇关于Android的报警器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆