一直在创建自定义通知,到目前为止已成功实现了通知,但是当我尝试使用我创建的通知上的按钮从MainActivity调用函数/方法时,我得到以下错误:
System services not available to Activities before onCreate()
以下是我在MainActivity中定义的方法,它更新通知UI并停止媒体播放器。
public void attachMediaActivity()
{
//INITIALIZE THE CONTEXT
context =this;
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
remoteViews=new RemoteViews(this.getPackageName(),R.layout.custom_notification);
remoteViews.setImageViewResource(R.id.notif_icon,R.drawable.stream_icon);
remoteViews.setTextViewText(R.id.notif_title,"stopped");
Intent button_intent= new Intent("button_clicked");
button_intent.putExtra("id",notification_id);
Intent notification_intent=new Intent(context,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,notification_intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder =new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setCustomBigContentView(remoteViews)
.setContentIntent(pendingIntent)
.setOngoing(true);
notificationManager.notify(notification_id,builder.build());
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
}
从主要活动调用上述方法的通知按钮附加的广播监听器。
public class Button_listener extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent)
{
NotificationManager manager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getExtras().getInt("id"));
Toast.makeText(context, "GENERATED BY NOTIFICATION", Toast.LENGTH_SHORT).show();
new MainActivity().attachMediaActivity();
}
}
logcat的:
01-24 11:52:29.010 13062-13062/com.amplitude.tron.samplemediaplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.amplitude.tron.samplemediaplayer, PID: 13062
java.lang.RuntimeException: Unable to start receiver com.amplitude.tron.samplemediaplayer.Button_listener: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2750)
at android.app.ActivityThread.access$1800(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1433)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:5288)
at com.amplitude.tron.samplemediaplayer.MainActivity.attachMediaActivity(MainActivity.java:159)
at com.amplitude.tron.samplemediaplayer.Button_listener.onReceive(Button_listener.java:21)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2743)
at android.app.ActivityThread.access$1800(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1433)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
我在哪里出错..在调用通知管理器之前,我已经将getPackageName()作为NULL 请帮忙!提前致谢
答案 0 :(得分:0)
new MainActivity().attachMediaActivity();
问题出在这里。与正常方式相反,使用new
标签创建Activity实例不会让您完成其应有的生命周期。
您可以通过设置捆绑包启动活动,使用Intent
传递它,启动startActivity
,最后检查活动中的捆绑值并调用attachMediaActivity
方法。或者,如果您愿意,可以在these ways中暂停当前的Activity实例并调用该方法。
答案 1 :(得分:0)
您明确指出的错误是,在onCreate()之前,系统服务无法用于活动
也就是说,如果不调用NotificationManager
,则无法使用Activity
中的startActivity(Intent)
。
您可以做的是,使用intent + extras启动活动以调用MainActivity中的方法。或者您可以使用Interfaces使用CallBack方法。
<强>更新强>
声明意图为:
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("attachMedia",true); // Extra info
context.startActivity(intent);
现在,在MainActivity onCreate()中,(在所有初始化之后)
boolean attachMedia = getIntent().getBooleanExtra("attachMedia",false);
if (attachMedia) {
attachMediaActivity();
}