package org.piaozhiye.demo;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class SDcardLinsener extends Activity {
SDcardLinsenerReceiver receiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter();
filter.addAction("org.piaozhiye.demo.sdcardLinsener.receiver");
filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
receiver = new SDcardLinsenerReceiver();
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
class SDcardLinsenerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()
.equals("android.intent.action.MEDIA_MOUNTED"))
{
Toast.makeText(getApplicationContext(),
"android.intent.action.MEDIA_MOUNTED",
Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(
"android.intent.action.MEDIA_REMOVED")) {
Toast.makeText(getApplicationContext(),
"android.intent.action.MEDIA_REMOVED",
Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(
"android.intent.action.MEDIA_UNMOUNTED")) {
Toast.makeText(getApplicationContext(),
"android.intent.action.MEDIA_UNMOUNTED",
Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(
"android.intent.action.MEDIA_BAD_REMOVAL")) {
Toast.makeText(getApplicationContext(),
"android.intent.action.MEDIA_BAD_REMOVAL",
Toast.LENGTH_SHORT).show();
}
}
}
}
android监听SD卡示例代码
最新推荐文章于 2024-09-02 18:19:21 发布