当前位置: 编程技术>移动开发
本页文章导读:
▪发送短信,发邮件,通电话代码实现 发送短信,发邮件,打电话代码实现
基础代码:
+ (void)alert:(NSString *)msg
{
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:msg message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:n.........
▪ Service将播音信息传递给 Service将广播信息传递给
BroadcastReceiver捕捉到信息后关闭服务
public class ServiceSendCast extends Activity {
/* 建立自定义mServiceReceiver对象 */
private MyServiceReceiver mReceiver01;
private TextView mTextView01;
pr.........
▪ BroadcastReceiver一个礼拜每天更换不同的壁纸 BroadcastReceiver一个星期每天更换不同的壁纸
添加权限
<uses-permission
android:name="android.permission.SET_WALLPAPER"></uses-permission>
三部分
1.DailyWallpaper.java
2.DailyReceiver.java(BroadcastReceiver)
3.Cha.........
[1]发送短信,发邮件,通电话代码实现
来源: 互联网 发布时间: 2014-02-18
发送短信,发邮件,打电话代码实现
基础代码:
打电话:
发短信:
发邮件:
基础代码:
+ (void)alert:(NSString *)msg { UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:msg message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease]; [alertView showWithBackground]; } + (NSString*) cleanPhoneNumber:(NSString*)phoneNumber { NSString* number = [NSString stringWithString:phoneNumber]; NSString* number1 = [[[number stringByReplacingOccurrencesOfString:@" " withString:@""] // stringByReplacingOccurrencesOfString:@"-" withString:@""] stringByReplacingOccurrencesOfString:@"(" withString:@""] stringByReplacingOccurrencesOfString:@")" withString:@""]; return number1; }
打电话:
+ (void) makeCall:(NSString *)phoneNumber { if ([DeviceDetection isIPodTouch]){ [UIUtils alert:kCallNotSupportOnIPod]; return; } NSString* numberAfterClear = [UIUtils cleanPhoneNumber:phoneNumber]; NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", numberAfterClear]]; NSLog(@"make call, URL=%@", phoneNumberURL); [[UIApplication sharedApplication] openURL:phoneNumberURL]; }
发短信:
+ (void) sendSms:(NSString *)phoneNumber { if ([DeviceDetection isIPodTouch]){ [UIUtils alert:kSmsNotSupportOnIPod]; return; } NSString* numberAfterClear = [UIUtils cleanPhoneNumber:phoneNumber]; NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"sms:%@", numberAfterClear]]; NSLog(@"send sms, URL=%@", phoneNumberURL); [[UIApplication sharedApplication] openURL:phoneNumberURL]; }
发邮件:
+ (void) sendEmail:(NSString *)phoneNumber { NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@", phoneNumber]]; NSLog(@"send sms, URL=%@", phoneNumberURL); [[UIApplication sharedApplication] openURL:phoneNumberURL]; } + (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body { NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@", to, cc, subject, body]; str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; }
[2] Service将播音信息传递给
来源: 互联网 发布时间: 2014-02-18
Service将广播信息传递给
BroadcastReceiver捕捉到信息后关闭服务
public class ServiceSendCast extends Activity { /* 建立自定义mServiceReceiver对象 */ private MyServiceReceiver mReceiver01; private TextView mTextView01; private Button mButton01, mButton02; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.service_send_cast); mTextView01 = (TextView) findViewById(R.id.myTextView1); mButton01 = (Button) findViewById(R.id.myButton1); mButton02 = (Button) findViewById(R.id.myButton2); /* 启动系统服务(Service) */ mButton01.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* 开始执行后台服务(Service) */ Intent i = new Intent(ServiceSendCast.this, SendService.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startService(i); mMakeTextToast("启动系统服务", false); } }); /* 终止系统服务(Service) */ mButton02.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* 指定终止执行后台服务(mService1) */ Intent i = new Intent(ServiceSendCast.this, SendService.class); /* 关闭系统服务 */ if (stopService(i) == true) { mMakeTextToast("已关闭系统服务", false); } else { /* 显示关闭服务失败 */ mTextView01.setText("关闭服务失败 "); } } }); } /* 建立继承告BroadcastReceiver的 mServiceReceiver类接受广播讯息 */ public class MyServiceReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { try { /* 取并来自后台服务所Broadcast的参数 */ Bundle bunde = intent.getExtras(); String strParam1 = bunde.getString("STR_PARAM1"); /* 将从Service里传来的广播讯息显示于TextView */ mTextView01.setText(strParam1); Intent i = new Intent(ServiceSendCast.this, SendService.class); if (stopService(i) == true) { mMakeTextToast("已关闭系统服务", true); } } catch (Exception e) { mTextView01.setText(e.toString()); e.getStackTrace(); } } } public void mMakeTextToast(String str, boolean isLong) { if (isLong == true) { Toast.makeText(ServiceSendCast.this, str, Toast.LENGTH_LONG).show(); } else { Toast.makeText(ServiceSendCast.this, str, Toast.LENGTH_SHORT) .show(); } } @Override protected void onResume() { try { /* 前景程序生命周期开始 */ IntentFilter mFilter01; /* 自定义要过滤的广播讯息(DavidLanz) */ mFilter01 = new IntentFilter(SendService.HIPPO_SERVICE_IDENTIFIER); mReceiver01 = new MyServiceReceiver(); registerReceiver(mReceiver01, mFilter01); } catch (Exception e) { mTextView01.setText(e.toString()); e.getStackTrace(); } super.onResume(); } protected void onPause() { /* 前景程序生命周期结束,解除刚守系统注册的Receiver */ unregisterReceiver(mReceiver01); super.onPause(); } }
public class SendService extends Service { private Handler objHandler = new Handler(); private int intCounter = 0; public static final String HIPPO_SERVICE_IDENTIFIER = "ComeFromServer"; /* 线程Tasks每1秒执行几次 */ private Runnable mTasks = new Runnable() { public void run() { intCounter++; /* 当后台Service执行5秒后,发生自定义的广播讯息 */ if (intCounter == 5) { Intent i = new Intent(HIPPO_SERVICE_IDENTIFIER); i.putExtra("STR_PARAM1", "来自Service的广播信息"); sendBroadcast(i); } Log.i("HIPPO", "Service Running Counter:" + Integer.toString(intCounter)); objHandler.postDelayed(mTasks, 1000); } }; public IBinder onBind(Intent arg0) { return null; } public void onStart(Intent intent, int startId) { /* 服务开始执行,启用线程 */ objHandler.postDelayed(mTasks, 1000); super.onStart(intent, startId); } public void onDestroy() { objHandler.removeCallbacks(mTasks); super.onDestroy(); } }
manifest.xml
<activity android:name=".ServiceSendCast" android:label="@string/app_name"> </activity> <service android:name=".SendService" android:exported="true" android:process=":remote"></service>
[3] BroadcastReceiver一个礼拜每天更换不同的壁纸
来源: 互联网 发布时间: 2014-02-18
BroadcastReceiver一个星期每天更换不同的壁纸
添加权限
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
三部分
1.DailyWallpaper.java
2.DailyReceiver.java(BroadcastReceiver)
3.ChangeBgImage.java
DailyWallpaper.java
public class DailyWallpaper extends Activity { private Button save; private Button cancel; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.daily_wallpaper); save = (Button) findViewById(R.id.button1); cancel = (Button) findViewById(R.id.button2); save.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { /* 取得服务启动后几天的0点0分0秒的millsTime */ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); long startTime = calendar.getTimeInMillis(); /* 重复执行的间隔时间 */ long repeatTime = 24 * 60 * 60 * 1000; /* 将更换桌布的排程叵丈AlarmManager中 */ Intent intent = new Intent(DailyWallpaper.this, DailyReceiver.class); PendingIntent sender = PendingIntent.getBroadcast( DailyWallpaper.this, 0, intent, 0); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); /* * setRepeating()让排程重复执行 startTime为开始执行时间 repeatTime为重复执行间隔 * AlarmManager.RTC?使服务休眠时仍然会执行 */ am.setRepeating(AlarmManager.RTC, startTime, repeatTime, sender); Toast.makeText(DailyWallpaper.this, "服务已启动", Toast.LENGTH_SHORT) .show(); /* 启动后马勺因执行几次换桌布的程序以更换今天的桌布 */ Intent intent2 = new Intent(DailyWallpaper.this , ChangeBgImage.class); startActivity(intent2); } }); cancel.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(DailyWallpaper.this, DailyReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(DailyWallpaper.this, 0, intent, 0); /* 由AlarmManager中移除排程 */ AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.cancel(sender); /* 以Toast提示已终止 */ Toast.makeText(DailyWallpaper.this, "服务已终止", Toast.LENGTH_SHORT) .show(); } }); }
DailyReceiver.java(BroadcastReceiver)
public class DailyReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Intent i = new Intent(context, ChangeBgImage.class); Bundle bundleRet = new Bundle(); bundleRet.putString("STR_CALLER", ""); i.putExtras(bundleRet); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } }
ChangeBgImage.java
public class ChangeBgImage extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 取得今天是星期几 */ Calendar ca=Calendar.getInstance(); int dayOfWeek=ca.get(Calendar.DAY_OF_WEEK)-1; Bitmap bitmap = null; switch(dayOfWeek){ case 0: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b01); break; case 1: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b02); break; case 2: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b03); break; case 3: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b04); break; case 4: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b05); break; case 5: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b06); break; case 6: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b07); break; } try { super.setWallpaper(bitmap); } catch (IOException e) { e.printStackTrace(); } finish(); } }
manifest.xml
<activity android:name=".DailyWallpaper" android:label="@string/app_name"> </activity> <activity android:name=".ChangeBgImage" android:label="@string/app_name"> </activity> <receiver android:name=".DailyReceiver" android:process=":remote" />
最新技术文章: