我想设置一个警报,在从Ubidots实时收到的值超过某个限制(此时,限制设置为10000)后将触发警报。 MainActivity.java中的部分代码是那些实现其他功能的代码(从Ubidots获取传感器值并在我的移动应用程序中将其绘制在图表中,此功能确实有效)
然而,警报未被触发。以下是我到目前为止所做的事情:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private static GraphicalView view;
private LineGraph line = new LineGraph();
private static Thread thread;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
thread = new Thread() {
public void run()
{
for (int i = 0; i <1000; i++)
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = UbidotsData.getDataFromReceiver(i); // We got new data!
line.addNewPoints(p); // Add it to our graph
view.repaint();
}
}
};
thread.start();
}
@Override
protected void onStart() {
super.onStart();
view = line.getView(this);
setContentView(view);
}
AlarmReceiver.java
public void scheduleAlarm(View V)
{
// Create an Intent and set the class which will execute when Alarm triggers
// Adding AlarmReceiver in the Intent, the onRecieve() method of this class will execute when
// alarm triggers
Intent intentFireAlarm = new Intent(this, AlarmReceiver.class);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
//set the alarm for particular time
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intentFireAlarm, 0);
//Ubidots Client
ApiClient api1 = new ApiClient("XXXXXXXXXXXXXXXXXXXX");
Variable ldr1 = api1.getVariable("XXXXXXXXXXXXXXXXXXXX");
Value[] values = ldr1.getValues();
int newData = (int) values[0].getValue();
if(newData>30000) {
alarmManager.set(alarmType, SystemClock.elapsedRealtime(), alarmIntent);
}
}
}
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Log.i(TAG,"Alarm Notification received");
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.teerna.smartagriculturev5">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
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=".AlarmReceiver"></receiver>
</application>
</manifest>
下面的这部分(取自Ubidots API)确实在移动应用程序的折线图中实时获取传感器值,因此我不确定这是否是一个问题:
ApiClient api1 = new ApiClient("XXXXXXXXXXXXXXXXXXXX");
Variable ldr1 = api1.getVariable("XXXXXXXXXXXXXXXXXXXX");
Value[] values = ldr1.getValues();
int newData = (int) values[0].getValue();