打电话
Intent intent1 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "13212312313"));
进网页
Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
选择音频软件
Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.setType("audio/*");
startActivity(Intent.createChooser(intent1,"选择音频软件"));
开机自启动
public class StartupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent mainIntent = new Intent(context, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent);
}
}
<receiver android:name=".StartupReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
显示当前手机电量
// 定义一个广播变量
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);
textView1.setText("电池用量: " + (level * 100 / scale) + "%");
}
context.unregisterReceiver(this);
}
};
//在onCreate里写这个
registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
接收短信信息
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] objArray = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[objArray.length];
for (int i=0;i<objArray.length;i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) objArray[i]);
}
for (SmsMessage currentMessage : messages) {
sb.append("短信来源:");
sb.append(currentMessage.getDisplayOriginatingAddress());
sb.append("\n---------短信内容--------\n");
sb.append(currentMessage.getDisplayMessageBody());
}
}
Intent mainIntent = new Intent(context, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent);
Toast.makeText(context, sb.toString(), Toast.LENGTH_SHORT).show();
}
}
}
<receiver
android:name=".SMSReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
自己发送广播让另外的软件接收到
//发送方
Intent addContactIntent = new Intent("net.blogjava.mobile.ADDCONTACT");
addContactIntent.putExtra("name", "aaa");
sendBroadcast(addContactIntent);
//接收方
public class AddContactReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("net.blogjava.mobile.ADDCONTACT".equals(intent.getAction())) {
String message = "";
Bundle bundle = intent.getExtras();
if (bundle != null) {
message = "name:"+bundle.getString("name");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
}
}
//接收方
<receiver android:name=".AddContactReceiver">
<intent-filter>
<action android:name="net.blogjava.mobile.ADDCONTACT" />
</intent-filter>
</receiver>