首先,我是Android App Development的初学者。我试图创建一个Android应用,告诉用户特定日期的时间表。但是,我想添加一个功能,其中应用程序会自动发送有关当时下一个讲座的通知。我尝试使用Android开发网站上提供的文档构建简单的通知消息,但代码似乎没有任何效果。我尝试过类似问题中提到的各种其他方法,但似乎都没有任何效果。 我发布了我的代码,这样你就可以告诉我我做错了什么。
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>
答案 0 :(得分:1)
我测试了您的代码,我能够在一段时间后看到通知。我想你可能错过了清单文件中提到的接收器。如果是这样,请添加以下内容。
<receiver
android:name=".MondayNotificationService">
</receiver>