2017-12-13 4 views
-6

申し訳ありません私は初心者ですが、なぜこのアクティビティを取得する際にエラーが発生しているのかわかりませんcontext行、これはコードですbroadcast receiverプログラム(アラーム)からringtoneを有効にしようとしています。なぜGetActivityコンテキストエラーが発生しますか?

public class OnAlarmReceiver extends BroadcastReceiver { 
private static final int NOTIFY_ME_ID = 1337; 

Ringtone ringTone; 
Uri uriRingtone; 


@Override 
public void onReceive(Context ctxt, Intent intent) { 
    SharedPreferences prefs = PreferenceManager 
      .getDefaultSharedPreferences(ctxt); 
    boolean useNotification = prefs.getBoolean("use_notification", true); 

    uriRingtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); 

    ringTone = RingtoneManager.getRingtone(getactivityContext(), uriRingtone); 
    ringTone.play(); 

    if (useNotification) { 
     NotificationManager mgr = (NotificationManager) 
       ctxt.getSystemService(Context.NOTIFICATION_SERVICE); 
     PendingIntent i = PendingIntent.getActivity(ctxt, 0, new Intent(ctxt, AlarmActivity.class), 0); 
     Notification note = new Notification.Builder(ctxt) 
       .setContentTitle("Restaurant List") 
       .setContentText("It's time for lunch!") 
       .setSmallIcon(android.R.drawable.stat_notify_chat) 
       .setContentIntent(i) 
       .setAutoCancel(true).build(); 
     note.flags |= Notification.FLAG_AUTO_CANCEL; 
     mgr.notify(NOTIFY_ME_ID, note); 
    } 
    else { 
     Intent i = new Intent(ctxt, AlarmActivity.class); 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     ctxt.startActivity(i); 
    } 
} 
} 
+4

変更getactivityContextのこの

ringTone = RingtoneManager.getRingtone(ctxt, uriRingtone); 

()トンo ctxt – MinnuKaAnae

答えて

1

使用する代わりに、この

ringTone = RingtoneManager.getRingtone(getactivityContext(), uriRingtone); 
+0

なぜ、それが愚かな質問であれば申し訳ありません – Novice

+0

ctxtとgetactivitycontext()の違いは何ですか? – Novice

+0

@Noviceこれは役立つでしょうhttps://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and –

関連する問題