2016-08-12 9 views
2

「subtotal_from_intent_putExtra_Hereを」注:私はAndroidサービスクラスから拡張サービスクラスへの意図の取得

活動クラス

public class UsherActivity extends ListActivity { 
    ... 

    public void createNotification() { 

     int subtotal = 580; 


     Intent serviceIntent = new Intent(this, SampleOverlayService.class); 
     serviceIntent.putExtra("box", "8"); 
     serviceIntent.putExtra("tot", subtotal); 
     startService(serviceIntent); 

    } 
} 

次のように私のSampleOverlayServiceクラスであるが、以下のようにサービスクラスにテントデータを渡すアクティビティクラスを持っている...これを解決

を助けてください...これはデータを表示したい場所です。私は正直に無駄に異なるアプローチを試してみた

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 

public class OverlayService extends Service { 

    protected boolean foreground = false; 
    protected boolean cancelNotification = false; 
    protected int id = 0; 

    protected Notification foregroundNotification(int notificationId) { 
     return null; 
    } 

    public void moveToForeground(int id, boolean cancelNotification) { 
     moveToForeground(id, foregroundNotification(id), cancelNotification); 
    } 

    public void moveToForeground(int id, Notification notification, boolean cancelNotification) { 
     if (! this.foreground && notification != null) { 
      this.foreground = true; 
      this.id = id; 
      this.cancelNotification = cancelNotification; 

      super.startForeground(id, notification); 
     } else if (this.id != id && id > 0 && notification != null) { 
      this.id = id; 
      ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(id, notification); 
     } 
    } 


    public void moveToBackground(int id, boolean cancelNotification) { 
     foreground = false; 
     id = 0; 
     super.stopForeground(cancelNotification); 
    } 

    public void moveToBackground(int id) { 
     moveToBackground(id, cancelNotification); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

} 

を次のように

import android.app.Notification; 
import android.app.PendingIntent; 

import android.content.Intent; 

public class SampleOverlayService extends OverlayService { 

    public static SampleOverlayService instance; 

    private SampleOverlayView overlayView; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     instance = this; 

     overlayView = new SampleOverlayView(this); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     if (overlayView != null) { 
      overlayView.destory(); 
     } 

    } 

    static public void stop() { 
     if (instance != null) { 
      instance.stopSelf(); 
     } 
    } 

    @Override 
    protected Notification foregroundNotification(int notificationId) { 
     Notification notification; 

     notification = new Notification(R.drawable.ic_launcher, "getString(R.string.title_notification), System.currentTimeMillis()); 

     notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_ONLY_ALERT_ONCE; 

     notification.setLatestEventInfo(this, "subtotal_from_intent_putExtra_Here", getString(R.string.message_notification), notificationIntent()); 

     return notification; 
    } 


    private PendingIntent notificationIntent() { 
     Intent intent = new Intent(this, SampleOverlayHideActivity.class); 

     PendingIntent pending = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     return pending; 
    } 

} 

そしてOverlayServiceクラスです。 intent.getExtras()...がどこに置かれてもnullを返すので、java.lang.nullPointerExceptionを取得します。通知に小計を入れるにはどうすればよいですか?

ありがとうございます。

編集:私はhttps://gist.github.com/bjoernQ/6975256

+0

intent.getExtras()をどこで呼び出すのですか? – fluffyBatman

+0

onStartCommandの関数をオーバーライドして、目的を得ることができる場所を指定します。 – has19

+0

@has 19はい、OverlayServiceクラスのonStartCommandに配信されていますが、SampleOverlayServiceクラスでそれを呼び出してStringに割り当てる方法はありますか? –

答えて

0

オーバーライドSampleOverlayServiceonStartCommand(Intent intent,, int flags, int startId)に配信されます。

コード:

public class SampleOverlayService extends OverlayService { 

private int totalCount = 0; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     totalCount = intent.getIntExtra("tot", 0); 
     return super.onStartCommand(intent, flags, startId); 
    } 

} 

これはtotalCount変数に値を取得し、それに応じて、この値を使用することができます。

+0

私は約1日前にそれを理解しましたが、あなたは正しいです...私はそれをあなたと同じ方法でやってくれました、精巧にしてくれてありがとう。 –

0

意図から基づかせていますが 'onStartCommand' メソッド

+0

はい、私はそれがonStartCommandのOverlayServiceクラス...しかし、私はそれをSampleOverlayServiceクラスで呼び出し、それをStringに代入しますか? –

関連する問題