AndroidのMVPを使用して、インストールされているすべてのアプリケーションのリストを取得する際に、モデルのアクティビティのコンテキストを使用する必要があります。 MVPパターンに従うときも同じです。ここでMVPのモデルのコンテキストが必要
はクラスである:
public class MainActivity extends BaseActivity
implements MainView,View.OnClickListener {
private MainPresenter mPresenter;
private Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
createPresenter();
}
private void init(){
sendButton= (Button) findViewById(R.id.button_send);
sendButton.setOnClickListener(this);
}
private void createPresenter() {
mPresenter=new MainPresenter();
mPresenter.addView(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_send:
mPresenter.onSendButtonClick();
break;
}
}
@Override
public void openOptionsActivity() {
Intent intent=new Intent(this,OptionsActivity.class);
startActivity(intent);
}
}
パブリッククラスMainPresenterはBasePresenter {
MainModel model;
public void onSendButtonClick(){
model.getListOfAllApps();
}
@Override
public void addView(MainView view) {
super.addView(view);
model=new MainModel();
}
}
public class MainModel {
public void getListOfAllApps(){
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
}
を}延び
getPackageManager()。queryIntentActivities(mainIntent、0)。ここにコンテキストがないとしてください。
1)は常にモデルからContext
を渡す:
'アプリケーションクラス'はありませんか? –
私はアプリケーションclass.butを使用する方法を知っています。アプリケーションクラスで使用するためにコンテキストにいくつかの定数フィールドを使用すると言っていますか?問題はモデルごとにアンドロイド固有のクラス/オブジェクトを使用しないでください。 –
全くありません。 onCreate(...)でapplicationContextを作成して使用します。 –