私がサービスの内側からトーストを呼び出すとき - タブタイトルに「ソースが見つかっていない」と「タイマークラス」というメッセージを含む新しいタブが表示されます
-
02-10-2019 - |
質問
Eclipseを使用して、デバッグモードでアプリを実行しています。私はクラスを持っています - Aastartshere、タバクティブから派生した。この派生クラスは、サービス(AASERVICE)を起動します。 AASERVICEは、タイマー/TimerTask aacallbackをセットアップします。このコールバック内から、通知を使用してトーストをセットアップします。 Aacallbackがトーストを呼び出すと、トーストに渡されたパラメーターが大丈夫に見えますが、画面には何も表示されず、代わりに新しいタブ(Timer.classというタイトル)が表示されます...
これがコードフラグメントです
AAStartsHere extends TabActivity {
:
Intent serviceIntent = new Intent (this, AAservice,...);
:
startservice(serviceIntent);
:
}
TimerTask execAACallback = newTimerTask { run() {AAcallback(); } };
AAService extends Service{
onCreate() {
:
AANotifcation = new Notification(....);
:
AATimer.scheduleAtFixedRate(execAACallback, ...)
}
AACallback() {
:
String svcName = Context.NOTIFICATION_SERVICE;
NotificationManager notiMgr = (NotificationManager) getSystemService(svcName);
Context context = getApplicationContext();
String text = "text goes here";
String title = "Title goes here";
Intent AAIntent = new Intent(AAService.this, AAStartsHere.class);
PendingIntent AAPendingIntent = PendingIntent.getActivity(context, 0, AAIntent, 0);
AANotification.setLatestEventInfo(context, title, text, AAPendingIntent);
AANotification.when = java.lang.System.currentTimeMillis();
notiMgr.notify(AA_NOTIFICATION_ID, AANotification);
Toast.makeText(context, title, Toast.LENGTH_LONG).show();
:
}
}
(Eclipse/Debugモードで)表示される新しいタブには、次のテキストクラスファイルエディターソースがあり、このクラスファイルのJARはコンテナ 'Android 2.1 "に属していることがわかりません。 Java(バージョン1.5:49.0、スーパービット):
あなたの考えを教えてください - 私は何が欠けていますか?助けてくれてありがとう。アビ
解決
表示されるトーストの時間間隔をカプセル化するのが長すぎる場合、トーストがサービスから表示されない場合があります。次のようなサービスのプロセスの最後にトーストのメッセージを表示してみてください。
AAStartsHere extends TabActivity {
:
Intent serviceIntent = new Intent (this, AAservice,...);
:
startservice(serviceIntent);
:
}
TimerTask execAACallback = newTimerTask { run() {AAcallback(); } };
AAService extends Service{
onCreate() {
:
AANotifcation = new Notification(....);
:
AATimer.scheduleAtFixedRate(execAACallback, ...)
}
AACallback() {
:
String svcName = Context.NOTIFICATION_SERVICE;
NotificationManager notiMgr = (NotificationManager) getSystemService(svcName);
Context context = getApplicationContext();
String text = "text goes here";
Intent AAIntent = new Intent(AAService.this, AAStartsHere.class);
PendingIntent AAPendingIntent = PendingIntent.getActivity(context, 0, AAIntent, 0);
AANotification.setLatestEventInfo(context, title, text, AAPendingIntent);
AANotification.when = java.lang.System.currentTimeMillis();
notiMgr.notify(AA_NOTIFICATION_ID, AANotification);
:
}
onStartCommand() {
String title = "Title goes here";
AACallBack();
Toast.makeText(context, title, Toast.LENGTH_LONG).show();
}
}
他のヒント
getApplicationContext()アクティビティではなく、アプリケーションクラスのインスタンスを返します。これを使用してトーストを表示することはできません。アクティビティのコンテキストを使用する必要があります。
所属していません StackOverflow