0

カスタム通知を作成していますが、これまで通知を正常に実装していますが、作成した通知のボタンを使用してMainActivityから関数/私は以下のエラーを取得:MainAcitivtyメソッドを呼び出すと通知ボタンがクリックされます

以下
System services not available to Activities before onCreate() 

通知UIを更新し、また、メディアプレーヤを停止MainActivityの内側に私によって定義された方法です。

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)  

私はこれまでimplemeted持っているもの: NOTIFICATION SNAPSHOT

はどこ私はとしてgetPackageNameを()取得されています。..も前に呼び出し通知マネージャに間違っているつもりですNULL 助けてください!事前に感謝

+0

あなたが 'Activity'をインスタンス化することはできません'new'とそれが正しく動作するようにしてください。 –

+2

@MikeM。はい、私はMainActivityを呼び出す場合でも、そのonCreateが呼び出されていないことを知っています。それはなぜソリューション/アドバイスのためにこのクエリを投稿したのですか? – IteratioN7T

+0

'new'でインスタンス化しようとすると' OnCreate'が呼び出されません – fluffyBatman

答えて

0
new MainActivity().attachMediaActivity(); 

問題がここにあります。通常の方法とは対照的に、newタグを使用してActivityのインスタンスを作成しても、それは想定されていたライフサイクルを通過することはできません。

バンドルを設定してIntentを渡してアクティビティを起動し、startActivityを開始し、最後にアクティビティのバンドル値を確認してattachMediaActivityメソッドを呼び出すことができます。または、必要に応じて、現在のActivityインスタンスをthese waysに保持し、メソッドを呼び出すことができます。

+0

こんにちは私は意図を使用したことはありません..私はこれが意味をなさないかもしれませんが? intent =新しいインテント(context、MainActivity.class);次はどこへ? コンテキストとインテントはonReceiveパラメータです! – IteratioN7T

+0

このhttps://developer.android.com/training/basics/firstapp/starting-activity.htmlに移動し、コンテンツを参照してください。あなたは、バンドルを持つインテントを通してアクティビティを開始する方法についてのアイデアを得るでしょう。 – fluffyBatman

+0

こんにちはfluffyBatman私は通知のボタンをクリックすると、古いアクティビティの上に再作成された取得に問題がある通知を取得することができました!上記のUIの変更が起こらないように、launchmodeを変更しようとすると、 – IteratioN7T

0

あなたは明らかに、システムサービスはonCreate()の前にアクティビティで利用できません。
つまりActivityNotificationManagerstartActivity(Intent)と呼ぶことなく使用することはできません。

あなたができることは、intent + extrasでアクティビティを開始して、MainActivity内でメソッドを呼び出すことです。または、インターフェイスを使用してコールバックメソッドを使用できます。

更新
宣言テントとして:

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(); 
} 
+0

こんにちはNizamこれまで行ってきたことです: intent = new Intent(context、MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); – IteratioN7T

+0

メインアクティビティ機能を呼び出すにはどうすればいいですか? PS:私はINTENTSを初めて利用しています – IteratioN7T

+0

更新された回答.. – Nizam

関連する問題