を見て:私はHEAP_LIMITはtriggered.Butは、それが働いていなかったようだし、ここにあるだろうかどうかを確認するためにデモを書いた https://developer.android.com/reference/android/app/ActivityManager.html#setWatchHeapLimit(long)ActivityManager.setWatchHeapLimitを使用する方法()の公式文書によるとheap_limitに
私のコード。
public class ActivityManagerActivity extends Activity
{
private String TAG = this.getClass().getName();
private Button btnIncreaseMemory;
private BroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_manager);
registerBroadcast();
registerHeapLimit();
iniComponent();
}
private void iniComponent()
{
btnIncreaseMemory = (Button) findViewById(R.id.btnIncreaseMemory);
btnIncreaseMemory.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Bitmap bitmap = ((BitmapDrawable) getDrawable(R.mipmap.image)).getBitmap();
Log.e(TAG, "bitmap size:" + bitmap.getByteCount());
((ImageView) findViewById(R.id.ivBitmap)).setImageBitmap(bitmap);
}
});
}
@Override
public void onDestroy()
{
super.onDestroy();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.clearWatchHeapLimit();
if (receiver != null)
{
unregisterReceiver(receiver);
receiver = null;
}
}
private void registerHeapLimit()
{
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.setWatchHeapLimit(2);
}
private void registerBroadcast()
{
IntentFilter filter = new IntentFilter();
filter.addAction("ACTION_REPORT_HEAP_LIMIT");
receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
// not be called
Toast.makeText(ActivityManagerActivity.this, "get msg", Toast.LENGTH_SHORT).show();
}
};
registerReceiver(receiver, filter);
}
}
私は、コードを変更します。
public class ActivityManagerActivity extends Activity
{
private String TAG = this.getClass().getName();
private Button btnIncreaseMemory;
private BroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_manager);
registerBroadcast();
registerHeapLimit();
iniComponent();
}
int index = 0;
private void iniComponent()
{
btnIncreaseMemory = (Button) findViewById(R.id.btnIncreaseMemory);
btnIncreaseMemory.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if(index == 0)
{
Bitmap bitmap = ((BitmapDrawable) getDrawable(R.mipmap.img6802)).getBitmap();
Log.e(TAG, "bitmap1 size:" + bitmap.getByteCount());
((ImageView) findViewById(R.id.ivBitmap)).setImageBitmap(bitmap);
index = 1;
}
else if(index == 1)
{
Bitmap bitmap = ((BitmapDrawable) getDrawable(R.mipmap.img6803)).getBitmap();
Log.e(TAG, "bitmap2 size:" + bitmap.getByteCount());
((ImageView) findViewById(R.id.ivBitmap2)).setImageBitmap(bitmap);
index = 2;
}
else if(index == 2)
{
Bitmap bitmap = ((BitmapDrawable) getDrawable(R.mipmap.img6804)).getBitmap();
Log.e(TAG, "bitmap3 size:" + bitmap.getByteCount());
((ImageView) findViewById(R.id.ivBitmap3)).setImageBitmap(bitmap);
index = 3;
}
}
});
}
@Override
public void onDestroy()
{
super.onDestroy();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.clearWatchHeapLimit();
if (receiver != null)
{
unregisterReceiver(receiver);
receiver = null;
}
}
private void registerHeapLimit()
{
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.setWatchHeapLimit(157286400);//150MB
}
private void registerBroadcast()
{
IntentFilter filter = new IntentFilter();
filter.addAction("ACTION_REPORT_HEAP_LIMIT");
receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
// not be called
Toast.makeText(ActivityManagerActivity.this, "get msg", Toast.LENGTH_SHORT).show();
}
};
registerReceiver(receiver, filter);
}
}
しかしまだ通知は、さえ使用-ヒープは、私が設定した限度の150メガバイトを可決しました。
apkをデバッグモードでビルドしましたか? –
@HasifSeydこんにちは、私は確信していませんが、私はデフォルトモードがデバッグmode.Cause私はASを使用していると思うし、android:debuggable = "true"はマニフェストXMLでは必要ありません。 – MichaelYe