简体   繁体   English

BroadcastReceiver 和 AlarmManager Android

[英]BroadcastReceiver and AlarmManager Android

I am trying to use an alarm manager with BroadcastReceiver.我正在尝试使用带有 BroadcastReceiver 的警报管理器。 I try to use the example given in Tutorial: System Services and BroadcastReceiver .我尝试使用教程:系统服务和 BroadcastReceiver 中给出的示例。 but when I run the example after the end of the time the toast doesn't show up.但是当我在时间结束后运行示例时,吐司没有出现。 the code is below:代码如下:

My main Activity:我的主要活动:

public class AlarmActivity extends Activity {


    /** Called when the activity is first created. */

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

    public void startAlert(View view) {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (i * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set in " + i + " seconds",
                Toast.LENGTH_LONG).show();
    }


}

my broadCastReceiver:我的广播接收器:

public class MyBroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Don't panic but your time is up!!!!.",
                Toast.LENGTH_LONG).show();
        // Vibrate the mobile phone
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);
    }

}

my main Layout:我的主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >

    <EditText
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Number of seconds"
            android:inputType="numberDecimal" >
    </EditText>

    <Button
            android:id="@+id/ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startAlert"
            android:text="Start Counter" >
    </Button>

</LinearLayout>

and the manifestfile:和清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.VIBRATE" >
    </uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.broadcastreceiver.AlarmActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <receiver android:name="com.example.android_alarm.MyBroadcastReceiver" >
        </receiver>
    </application>

</manifest>

The receiver must extend from BroadcastReceiver接收器必须从 BroadcastReceiver 扩展

public class MyBroadcastReceiver extends BroadcastReceiver {
    // ...
}

Also be sure that receiver name in manifest file is correct.还要确保清单文件中的接收者名称是正确的。

Your MyBroadcastReceiver class is wrong, edit your MyBroadcastReceiver class to the following:您的MyBroadcastReceiver类是错误的,将您的MyBroadcastReceiver类编辑为以下内容:

public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
         Toast.makeText(context, "Don't panic but your time is up!.",
         Toast.LENGTH_LONG).show();
         // Vibrate the mobile phone
         Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
         vibrator.vibrate(2000);    
    }
}

You need to add extends BroadcastReceiver and override onReceive() method.您需要添加extends BroadcastReceiver并覆盖 onReceive() 方法。

You just forgot to override OnReceive() method.您只是忘记覆盖OnReceive()方法。 public void onReceive(Context context, Intent intent) is predefined method of BroadcastReceiver, wheneven you are using it, You must @Override it as follows, public void onReceive(Context context, Intent intent)是 BroadcastReceiver 的预定义方法,即使你正在使用它,你也必须@Override 它如下,

public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override   // You forgot this line 
    public void onReceive(Context context, Intent intent) 
    {
          Toast.makeText(context, "Don't panik but your time is up!!!!.",Toast.LENGTH_LONG).show();
    }
}

你已经创建了一个你没有在任何地方调用的 startAlert 函数。所以首先在 onCreate 中调用该方法,然后你将收到一个祝酒词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM