2014-04-29 4 views
-4

Intent.FLAG_ACTIVITY_NEW_TASK 브로드 캐스트 메시지가 수신되면 특정 시간 간격 후에 동작이 트리거되는 안드로이드 응용 프로그램에서 작업하고 있습니다. 나는 UpdateService.java에 다음 코드를 가지고 :활동이 호출되지 않습니다.

package com.missnoob.screentimeout; 

import android.app.Service; 
import android.content.BroadcastReceiver; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

public class UpdateService extends Service { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC 
     IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
     filter.addAction(Intent.ACTION_SCREEN_OFF); 
     BroadcastReceiver mReceiver = new ScreenReceiver(); 
     registerReceiver(mReceiver, filter); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     boolean screenOn = intent.getBooleanExtra("screen_state", false); 
     if (!screenOn) { 
      Log.v("ScreenTimeOut","Broadcast Received"); 
      Toast.makeText(getApplicationContext(), "Broadcast Received", Toast.LENGTH_LONG).show(); 

      //This part is not working 
      Intent i = new Intent(this, Notification.class); 
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      //This part is not working 

     } 
     else 
     { 
      // YOUR CODE 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

그리고 에 다음 코드 Notification.java :

package com.missnoob.screentimeout; 

import java.util.Timer; 
import java.util.TimerTask; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 
import com.shadow.screentimeout.R; 

public class Notification extends Activity { 
/** Called when the activity is first created. */ 

Timer timer; 
Toast toast; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    timer = new Timer(); 
    toast = Toast.makeText(getApplicationContext(), "15 seconds after",Toast.LENGTH_SHORT); 

    timer.scheduleAtFixedRate(new TimerTask() 
     { 
      @Override 
      public void run() { 
       toast.show(); 
       Log.v("ScreenTimeOut","Toast showed"); 

      } 
     }, 0, 5000); 

    } 

} 

Intent i = new Intent(this, Notification.class); 
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
을 UpdateService.java 에서

이 실행되지 않습니다. 그것은 다른 스레드에서 실행되는 경우에만 UI 스레드에서 UI를 업데이트 할 수

+2

'만약 startActivity (I)'?? – Raghunandan

+0

이것은 간단한 구글 검색으로 알아낼 수 있습니다 :'나는 어떻게 안드로이드에서 새로운 활동에 가야합니까? ' –

답변

1
당신은 startActivity(i)

Intent i = new Intent(this, Notification.class); 
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(i); 

누락

은 또한 타이머에

toast.show(); 

을 가질 수 없습니다.

대신 Handler을 사용하십시오. 당신은 당신은 startActivity(i)을 호출되지 않은

Android Thread for a timer

+0

다른 스레드에있는 Timer에서도 그 토스트를 보여줍니다. –

+0

@kalyanpvs가 (가) 댓글을 올리기 전에 업데이트되었습니다. – Raghunandan

+0

이제 완벽한 ... 나의 측면에서 +1. –

1

@ 예를 찾을 수 있습니다.

Start Another Activity (Android 개발자 웹 사이트)을 참조하십시오.

1

당신은 startActivity(i)를 호출하지 않았다, 당신의 활동이 caled되지 않습니다 왜, 당신이해야 할 모든이처럼 먹으 렴 :

Intent i = new Intent(this, Notification.class); 
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(i); 
관련 문제