我正在制作一个闹钟,它会在所需的时间后显示通知。为此,我使用alarmmanager和broadcastreceiver来显示通知。 我使用通知构建器来构建通知。我的问题是broadcastreceiver没有在给定时间显示通知。
MainActivty.java
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button1);
final EditText et = (EditText)findViewById(R.id.et);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s = et.getText().toString();
int time = Integer.parseInt(s);
setAlarm(time);
}
});
}
public void setAlarm(int time){
i++;
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), i, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (time * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + time + " seconds",
Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver.java
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/**Toast.makeText(context, "Time is up!!!!.",
Toast.LENGTH_LONG).show();
// Vibrate the mobile phone
Vibrator vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
**/
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context)
// Set Icon
//.setSmallIcon(R.drawable.logosmall)
// Set Ticker Message
.setTicker("ticker")
// Set Title
.setContentTitle("new Notification")
// Set Text
.setContentText("new message")
// Add an Action Button below Notification
//.addAction(R.drawable.ic_launcher, "Action Button", pIntent)
// Set PendingIntent into Notification
//.setContentIntent(pIntent)
// Dismiss Notification
.setAutoCancel(true);
NotificationManager notificationmanager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
}
xml layout
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.alarm.MainActivity" >
<EditText
android:id="@+id/et"
android:layout_width="100dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_below="@+id/timePicker1"
android:layout_marginLeft="89dp"
android:layout_marginTop="26dp"
android:text="Button" />
</RelativeLayout>
Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarm"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
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="MyBroadcastReceiver" >
</receiver>
</application>
</manifest>
我希望问题很清楚:)
答案 0 :(得分:2)
好的,我认为问题在于,您需要设置小图标。没有它,通知根本不会显示。