Reputation: 25
First of all, I'm a beginner in Android App Development. I'm trying to make an Android app that tells the user the time table for a particular day. However, I want to add a feature wherein the app automatically sends a notification about the next lecture at that time. I've tried to build a simple notification message using the documentation available on the Android Development website but the code seems to have no effect. I've tried various other methods mentioned in similar questions but none seem to have any effect. I'm posting my code so you can tell me what I'm doing wrong.
Monday.java
package com.arc.sid.timetable;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
public class Monday extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monday);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView periods = (ListView) findViewById(R.id.mondayList);
periods.setAdapter(new CustomBaseAdapter(this, searchResults));
periods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object period = periods.getItemAtPosition(position);
SearchResults fullObject = (SearchResults) period;
Toast.makeText(Monday.this, "Period : " + " " + fullObject.getPeriodName() + "\nLecturer : "
+ " " + fullObject.getFaculty() + "\nTimings : " + " " + fullObject.getTiming(), Toast.LENGTH_LONG).show();
}
});
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MondayNotificationService.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// set for 30 seconds later
alarmMgr.set(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis() + 30000, alarmIntent);
}
MondayNotificationService.java
package com.arc.sid.timetable;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;
public class MondayNotificationService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("Random title")
.setContentText("Random text")
.setSmallIcon(R.drawable.icon)
.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, Monday.class), 0))
.build();
notificationManager.notify(0, notification);
}
}
activity_monday.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="10dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/activity_monday"
tools:context="com.arc.sid.timetable.Monday">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mondayList"
/>
</LinearLayout>
Upvotes: 1
Views: 680
Reputation: 2083
I tested your code and I am able to see the notification after a certain time. I think you may have missed mentioning the receiver in the manifest file. Add following if it is so.
<receiver
android:name=".MondayNotificationService">
</receiver>
Upvotes: 1