安卓闹钟不工作

发布于 2024-10-11 15:09:13 字数 982 浏览 6 评论 0原文

我已经为此苦苦挣扎了几个小时。我还检查了文档和几个主题。我在两个主题中找到了这段代码,两个人都说该代码运行完美,但在我的计算机上却不行。第一个 Toast 出现,但第二个 Toast 从未出现。怎么了?

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();

    }
 }

}

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();

    }
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨后彩虹 2024-10-18 15:09:13

实际上,您不需要指定操作,因为您在意图中使用了类 AlarmReceiver.class

在您的 AndroidManifest.xml 中,确保您在 标记内有一个接收器定义,例如:

编辑:
好的,有两种方法可以使用广播接收器。

1) 从您提供的代码中,AlarmReceiver.java 将包含:

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();

}

}

像这样,您可以将广播接收器设置为与 AndroidManifest.xml 和标签

2) 第二种方式。通过这种方式,您只需使用 1 个文件 HelloWorld2.java

在您的活动中,创建广播接收器并注册它。

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();


    }

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

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

<receiver android:name="AlarmReceiver">

Edit:
Ok there are 2 ways to use your broadcast receiver.

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();

    }
 }

and 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();

}

}

Like this, you can set your broadcast receiver to work with the AndroidManifest.xml and the tag <receiver ...>

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();


    }
猫腻 2024-10-18 15:09:13

我遇到了同样的问题,直到我发现我将广播接收器放在了不同的包上,而不是通用的包上。

简单地更改

<receiver android:name=".AndroidAlarmService" android:enabled="true" >

为:

<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >

I had the same problem until I found that I had put my Broadcast Receiver on a different package, not the general.

Simply changed:

<receiver android:name=".AndroidAlarmService" android:enabled="true" >

for:

<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >
我早已燃尽 2024-10-18 15:09:13

如果上述答案对您不起作用,那么还有另一种方法可以在 AlarmManager 触发过期警报时接收任何回调。您只需检查一下这一点:通过在 PendingIntent 实例化时发送错误的Intent。例如,您希望在其中一个接收器上接收 onReceive 调用,但您通过 getActivitygetService 实例化了 PendingIntent >,但你真正的意思是getReceiver

创建PendingIntent实例时,有多种创建方法(getServicegetActivitygetReceiver>getForegroundService

如果您想要 Activity 意图的接收者,那么您:

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*);

如果您想要 BroadcastReceiver 意图的接收者:

PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);

如果您想要前台Service 意图的接收者:

PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);

如果您想要一个 Service 意图的接收者:

PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);

另外,请确保您的意图指向正确的类(例如为 Activity 创建意图,如果您像这样错误地通过,您将不会收到任何电话:

Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver

// PendingIntent was created in such a way 
// you wanted this to be received by an activity. 
// you will not receive any call if you set it up like this.
PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 

我也发布了类似的答案此处

。HTH

If the answer above doesn't work for you then there is another way to not receive any callbacks when AlarmManager fires an expired alarm. You simply need to check this one out: by sending the wrong Intent on instantiation of PendingIntent. For example you wanted to receive a call onReceive on one of your receivers but you instantiated a PendingIntent via getActivity or getService, but what you actually meant is getReceiver.

When creating instance of PendingIntent, there are many ways to create it (getService, getActivity,getReceiver, getForegroundService:

if you want Activity the receiver of the intent then you:

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*);

if you want BroadcastReceiver the receiver of the intent:

PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);

if you want a foreground Service the receiver of the intent:

PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);

if you want a Service the receiver of the intent:

PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);

Also, make sure you intents are pointing to the correct class. (e.g. creating intents for Activity, Service etc.). You will not receive any call if you pass wrongfully like this:

Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver

// PendingIntent was created in such a way 
// you wanted this to be received by an activity. 
// you will not receive any call if you set it up like this.
PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 

I also posted similar answer here.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文