android 常用代码---常规

来源:互联网 发布:微信公众号矩阵 编辑:程序博客网 时间:2024/05/16 10:23

1 发送SMS
权限 <uses-permission android:name="android.permission.SEND_SMS"/>

代码 SmsManager m = SmsManager.getDefault();

String destinationNumber ="0123456789";
String text = "Hello, MOTO!";
m.sendTextMessage(destinationNumber, null, text, null, null);


2 显示Toast

权限
代码 Toast.makeText(this, "Put your message here", Toast.LENGTH_SHORT).show();


3 状态栏通知


权限
代码 int notificationID = 10;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Create the notification
Notification notification = new Notification(R.drawable.yourIconId, "Put your notification text here", System.currentTimeMillis());
// Create the notification expanded message
// When the user clicks on it, it opens your activity
Intent intent = new Intent(this, YourActivityName.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "Put your title here", "Put your text here", pendingIntent);
// Show notification
notificationManager.notify(notificationID, notification);



4 在给定时间内震动手机

权限 <uses-permission android:name="android.permission.VIBRATE"/>
代码 // Vibrate for 1000 miliseconds

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);


5 在开关模式后震动手机

权限 <uses-permission android:name="android.permission.VIBRATE"/>
代码 // Vibrate in a Pattern with 0ms off(start immediately), 200ms on, 100ms off, 100ms on, 500ms off, 500ms on,
// repeating the pattern starting from index 4 - 100ms on.
// Note that you'll have to call vibrator.cancel() in order to stop vibrator.
// Change second parameter to -1 if you want play the pattern only once.
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(new long[] {0, 200, 100, 100, 500, 500}, 4);






原创粉丝点击