0

我想從BroadcastReceiver開始活動。問題是,當我嘗試啓動它時,我收到一個異常,它告訴我添加「FLAG_ACTIVITY_NEW_TASK」標誌,因爲我的Receiver已註冊到Service中,而不是註冊到Activity中。 所以,我修改了代碼到廣播接收器類:Android:從BroadcastReceiver開始活動

public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
    Intent start=new Intent(context,MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(start); 
} 

但我的活動不會啓動。有沒有人可以解釋我爲什麼得到這種行爲?

回答

0

我想這只是一個錯字。

您必須致電start.addFlags()而不是intent.addFlags()

+0

是的,那是我的(愚蠢的)問題! – user1071138

+0

@ user1071138如果這解決了你的問題,爲什麼你不接受這個答案;) – tnj

1

我認爲這個問題是標誌,它是這樣使用

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

,但你已經使用addFlags()功能。

1

您可以使用此: -

@Override 
public void onReceive(Context context, Intent intent) { 


    intent.setClass(context, nextgoingactivity); 
    intent.putExtra("sipcallid", sipAddress); // your data 
intent.putExtra("sipAddress", sipcallid); // your data 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent); 

} 

和更新AndroidManifesh.xml,

<receiver android:name=".IncomingCallReceiver" android:label="Call Receiver"> 
     <intent-filter> 
      <action android:name=".nextgoingactivity"/> 
     </intent-filter> 
    </receiver> 

應該解決您的查詢!