2011-09-27 106 views
7

我有一個活動,當手機響起時(通過電話應用程序),我顯示爲非模態。當發生以下任一事件時,我想完成活動。第一個是如果我觸摸活動外的任何地方(這不是問題),第二個是如果振鈴停止。我正在收聽廣播接收機中的IDLE_STATE,但我不確定如何在看到它時調用活動的結束。接收器未通過活動註冊,但通過Manifest.xml註冊從廣播接收器完成活動

回答

0

如果從活動註冊另一個廣播接收器,該怎麼辦?然後,當你想殺死它時,從你提到的廣播接收器發送廣播消息。

0

我實際上最終在活動中添加了一個PhoneStateListener來監聽IDLE_STATE。

12

把代碼寫在你的接收廣播現在這將發送一個名爲「com.hello.action」的意圖另一撒施你想完成它

Intent local = new Intent(); 
local.setAction("com.hello.action"); 
sendBroadcast(local); 

現在趕上這個意圖在活動中和然後調用super.finish()接收器上 的方法的onReceive這樣

public class fileNamefilter extends Activity { 
ArrayAdapter<String> adapter; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    IntentFilter filter = new IntentFilter(); 

    filter.addAction("com.hello.action"); 
    registerReceiver(receiver, filter); 

    } 
BroadcastReceiver receiver = new BroadcastReceiver() { 

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

    } 
}; 

public void finish() { 
    super.finish(); 
}; 
} 

這將完成活動