我遇到了一些问题,结帐天气设备是否与PC连接?所以我发现意图动作ACTION_UMS_CONNECTED
用于获取标志以检查天气设备是否与PC连接。
我试过一个例子,但我无法得到任何旗帜......我在这里有过去的代码
package com.Mediamount;
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.telephony.TelephonyManager;
import android.widget.Toast;
public class Mediamount extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter();
this.registerReceiver(mIntentReceiver, filter);
}
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
boolean queryRestart = false;
if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
Toast.makeText(getBaseContext(), "asdasd", Toast.LENGTH_LONG).show();
} else if (action.equals(Intent.ACTION_SCREEN_ON)) {
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
} else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
}
}
};
}
清单文件中是否有任何遗漏,请告诉我。
更新
package com.Mediamount;
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 Mediamount extends Activity {
public static BroadcastReceiver mReceiver1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter mIntentFilter = new IntentFilter(Intent.ACTION_UMS_CONNECTED);
// install an intent filter to receive SD card related events.
IntentFilter intentFilter1 = new IntentFilter
(Intent.ACTION_MEDIA_MOUNTED);
intentFilter1.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter1.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter1.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter1.addAction(Intent.ACTION_MEDIA_EJECT);
// install an intent filter to receive UMS(USB) related events.
intentFilter1.addAction(Intent.ACTION_UMS_CONNECTED);
intentFilter1.addAction(Intent.ACTION_UMS_DISCONNECTED);
intentFilter1.addAction("USB_INTENT");
intentFilter1.addDataScheme("file");
mReceiver1 = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, context.toString(),Toast.LENGTH_LONG).show();
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
//Toast.makeText(context, "SD Card mounted",Toast.LENGTH_LONG).show();
} else if (action.equals
(Intent.ACTION_MEDIA_UNMOUNTED)) {
// Toast.makeText(context, "SD Card unmounted",Toast.LENGTH_LONG).show();
} else if (action.equals
(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
// Toast.makeText(context, "SD Card scanner started",Toast.LENGTH_LONG).show();
//System.out.println("SD Card scanner started");
} else if (action.equals
(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
// Toast.makeText(context, "SD Card scanner finished",Toast.LENGTH_LONG).show();
} else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
// Toast.makeText(context, "SD Card eject",Toast.LENGTH_LONG).show();
} else if(action.equals(Intent.ACTION_UMS_CONNECTED))
{
// Toast.makeText(context, "connected",Toast.LENGTH_LONG).show();
} else if(action.equals
(Intent.ACTION_UMS_DISCONNECTED)) {
// Toast.makeText(context, "disconnected",Toast.LENGTH_LONG).show();
}
// BONG_TEST }
}
};
registerReceiver(mReceiver1, intentFilter1);
}}