好的,我已经设置了一个警报管理器,每10秒激活一次.我用吐司测试了它,它工作得很好,但我想要显示通知.我不能让这个代码编译.所有错误都发生在// NOTIFICATION之后,直到wl.release();
错误:
The constructor NotificationCompat.Builder(Alarm) is undefined
The constructor Intent(Alarm, Class<StatusOfChild>) is undefined
The method create(Context) in the type TaskStackBuilder is not applicable for the arguments (Alarm)
The method getSystemService(String) is undefined for the type Alarm
Run Code Online (Sandbox Code Playgroud)
码:
package com.sjjgames.abortionappnoads;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.Toast;
public class Alarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock …Run Code Online (Sandbox Code Playgroud) 我在通知管理器中调试一个变量,因为第一次意图成功运行但第二次当我意图新消息时,活动显示thr perivous值plz帮助我实际上是一个大问题
- 这是通知管理器的代码
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
displayMessage(context, "Your device registred with GCM");
Log.d("NAME", MainActivity.name);
ServerUtilities.register(context, MainActivity.name, MainActivity.email, registrationId);
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
displayMessage(context, getString(R.string.gcm_unregistered));
ServerUtilities.unregister(context, registrationId);
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = …Run Code Online (Sandbox Code Playgroud) 我已经阅读了关于这个主题的其他一些帖子,我认为我的代码听起来应该是警报,但事实并非如此.它确实振动,但没有声音.关于如何传达声音的任何建议?
该程序的另一部分是能够播放铃声所以问题似乎是具体的这个例程.
这是一个扩展Service的类
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (sound == null) { Log.i("RECEIVER", "SOUND IS NULL"); }
NotificationManager myNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intentMain = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intentMain, 0);
long[] pattern = {500,500,500,500,500,500,500,500,500};
Notification myNote = new Notification.Builder(this)
.setContentTitle("NotificationDemo")
.setContentText("NotificatinDemo")
.setSmallIcon(R.drawable.ic_launcher)
.setSound(sound)
.setVibrate(pattern)
.setContentIntent(pIntent)
.build();
myNM.notify(1, myNote);
return super.onStartCommand(intent, flags, startId);
}
Run Code Online (Sandbox Code Playgroud) 当我尝试在片段类中为Notificatoin Manager分配值时,我有Null Pointer Exception.
private NotificationManager m_notificationMgr;
public void CreateNotification() {
Log.d(TAG, "created");
m_stopwatch = new Stopwatch();
m_notificationMgr = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//null pointer exception
createNotification();
Log.d(TAG, "ok");
}
Run Code Online (Sandbox Code Playgroud)
错误日志
05-15 12:31:16.127: E/AndroidRuntime(2216): FATAL EXCEPTION: main
05-15 12:31:16.127: E/AndroidRuntime(2216): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timetracker/com.example.timetracker.MainActivity}: java.lang.NullPointerException
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.os.Looper.loop(Looper.java:137)
05-15 12:31:16.127: E/AndroidRuntime(2216): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-15 12:31:16.127: …Run Code Online (Sandbox Code Playgroud) 我刚刚导入了一个android项目,并且在Notification部分中,我必须使用setLatestEventInfo。但是Android Studio说无法解析方法setLatestEventinfo。这是我的代码段,请在回答时帮助您编辑我的代码
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0);
Intent notify_intent = new Intent(context,SpecialSerivceReportActivity.class);
notify_intent.putExtra("filename", "Special Service Report");
notify_intent.putExtra("URL", urls[0]);
PendingIntent contentIntent = PendingIntent.getActivity(context,(int)System.currentTimeMillis(),notify_intent,PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Service Report", contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_SOUND ;
nm.notify((int)System.currentTimeMillis(),notification);
}
else if(message.equalsIgnoreCase("Special Columnist Blog"))
{
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0);
Intent notify_intent = new Intent(context,SpecialSerivceReportActivity.class);
notify_intent.putExtra("filename", "Special Columnist Blog");
notify_intent.putExtra("URL", urls[1]);
PendingIntent contentIntent = PendingIntent.getActivity(context,(int)System.currentTimeMillis(),notify_intent,PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, "CEPF Mobile", "New …Run Code Online (Sandbox Code Playgroud) package com.example.khatrimann.notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
b.setTicker("khatrimann")
.setContentTitle("Default notification")
.setContentText("Hey There.")
.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
}
Run Code Online (Sandbox Code Playgroud)
此代码在marshmallow上进行测试,它崩溃并且无法正常工作.
android.support.v4和v7,两个都是逐个尝试然后应用程序崩溃.
LOGCAT如下:
11-20 18:24:59.623 4146-4146/com.example.khatrimann.notification E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.khatrimann.notification, PID: 4146
Theme: …Run Code Online (Sandbox Code Playgroud)