欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android studio 3.0 service項目背景音樂實現(xiàn)

 更新時間:2017年11月17日 14:48:23   投稿:laozhang  
這篇文章主要介紹了android studio 3.0中service項目實現(xiàn)插入背景音樂的方法。

這篇文章是博主在通過學(xué)習(xí)Android Studio的同時,實現(xiàn)service項目中用于背景音樂的實現(xiàn),郵件的發(fā)送用于隨堂小測的發(fā)送郵件功能。其中也碰到需要坑和錯誤,最后都解決了,一起跟著學(xué)習(xí)一下吧。如果大家有更好的方法可以在下面的留言區(qū)討論。

本次項目我主要負(fù)責(zé)Android studio的后端,以及游戲文案游戲策劃,結(jié)果后來事情太散了,Android studio學(xué)的不咋地,文案寫完還有幫著寫一寫數(shù)據(jù)庫的插入語句,然后就是跟隊友完成了as的后臺插入聲音的代碼。接下來介紹的service項目中用于背景音樂的實現(xiàn),郵件的發(fā)送用于隨堂小測的發(fā)送郵件。

開發(fā)基礎(chǔ)之Service

Activity可以呈現(xiàn)一個用戶界面,但是Service運行在后臺,試了以下實例,啟動Service,并通過從Activity向Service傳遞數(shù)據(jù)。

package com.example.lhb.startservice; 
 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.view.ViewDebug; 
import android.widget.Toast; 
 
public class MyService extends Service { 
  private boolean Running=false; 
  private String data="默認(rèn)信息?。?!"; 
  public MyService() { 
  } 
 
  @Override 
  public IBinder onBind(Intent intent) { 
    // TODO: Return the communication channel to the service. 
    throw new UnsupportedOperationException("Not yet implemented"); 
  } 
 
  @Override 
  public int onStartCommand(Intent intent, int flags, int startId) {  
    data=intent.getStringExtra("data");//這里的intent是參數(shù)里的,不是自定義的 
    return super.onStartCommand(intent, flags, startId); 
  } 
 
  @Override 
  public void onCreate() { 
    super.onCreate(); 
    Running=true; 
    new Thread(){ 
      @Override 
      public void run() { 
        super.run(); 
        while (Running){ 
          System.out.println(data); 
          try { 
            sleep(3000); 
          } catch (InterruptedException e) { 
            e.printStackTrace(); 
          } 
        } 
      } 
    }.start(); 
  } 
 
  @Override 
  public void onDestroy() { 
    super.onDestroy(); 
    Running=false; 
  } 
} 
//主代碼
package com.example.lhb.startservice; 
 
import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 
 
 
public class MainActivity extends ActionBarActivity { 
  private EditText inputText; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
 
    findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        inputText= (EditText) findViewById(R.id.inputText); 
        if(inputText.getText().length()==0){ 
          Toast.makeText(MainActivity.this,"請輸入傳遞的值!",Toast.LENGTH_SHORT).show(); 
          return; 
        } 
        Intent intent; 
        intent=new Intent(MainActivity.this,MyService.class); 
        intent.putExtra("data",inputText.getText().toString()); 
        startService(intent); 
      } 
    }); 
 
    findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        Intent intent; 
        intent=new Intent(MainActivity.this,MyService.class); 
        stopService(intent); 
      } 
    }); 
  } 
} 

 

以此來完成Activity向Service傳遞數(shù)據(jù)的任務(wù)。
之后嘗試了as中間去實現(xiàn)音樂播放器,參考第一行代碼p303-307。
先寫入布局代碼,三個按鈕用來播放,停止,暫停

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

  <TextView android:text="音頻播放器" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="播放"
      android:id="@+id/button"
      android:layout_weight="0.33" />

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="停止"
      android:id="@+id/button2"
      android:layout_weight="0.33" />

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="暫停"
      android:id="@+id/button3"
      android:layout_weight="0.33" />
  </LinearLayout>

</RelativeLayout>

最后將service與音頻播放結(jié)合,寫了一個可以再主界面播放的背景音樂:
此界面一打開就有音樂:

開發(fā)過程學(xué)到的郵件發(fā)送

這個在上一次的隨堂小測中間有用到。

public class Main {
  public static String myEmailAccount = "929585831@qq.com";
  public static String myEmailPassword = "uhszzhgojydfbbec";  // 授權(quán)碼

  public static String myEmailSMTPHost = "smtp.qq.com";

  // 收件人郵箱
  public static String receiveMailAccount = "541227688@qq.com";

  public static void main(String[] args) throws Exception {
    // 1. 創(chuàng)建參數(shù)配置, 用于連接郵件服務(wù)器的參數(shù)配置
    Properties props = new Properties();          // 參數(shù)配置
    props.setProperty("mail.transport.protocol", "smtp");  // 使用的協(xié)議(JavaMail規(guī)范要求)
    props.setProperty("mail.smtp.host", myEmailSMTPHost);  // 發(fā)件人的郵箱的 SMTP 服務(wù)器地址
    props.setProperty("mail.smtp.auth", "true");      // 需要請求認(rèn)證
    
    // SMTP 服務(wù)器的端口 ,
    //         需要改為對應(yīng)郵箱的 SMTP 服務(wù)器的端口, 具體可查看對應(yīng)郵箱服務(wù)的幫助,
    //         QQ郵箱的SMTP(SLL)端口為465或587, 其他郵箱自行去查看)
    final String smtpPort = "465";
    props.setProperty("mail.smtp.port", smtpPort);
    props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.setProperty("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.socketFactory.port", smtpPort);
    
    // 2. 根據(jù)配置創(chuàng)建會話對象, 用于和郵件服務(wù)器交互
    Session session = Session.getInstance(props);
    session.setDebug(true);                 // 設(shè)置為debug模式, 可以查看詳細(xì)的發(fā)送 log


    int i=0;                       //寫了個小循環(huán)舍友連收30份垃圾郵件emmm
    for(i=0;i<30;i++) {
      // 3. 創(chuàng)建一封郵件
      MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount);

      // 4. 根據(jù) Session 獲取郵件傳輸對象
      Transport transport = session.getTransport();

      // 5. 使用 郵箱賬號 和 密碼 連接郵件服務(wù)器, 這里認(rèn)證的郵箱必須與 message 中的發(fā)件人郵箱一致, 否則報錯
      transport.connect(myEmailAccount, myEmailPassword);

      // 6. 發(fā)送郵件, 發(fā)到所有的收件地址, message.getAllRecipients() 獲取到的是在創(chuàng)建郵件對象時添加的所有收件人, 抄送人, 密送人
      transport.sendMessage(message, message.getAllRecipients());

      // 7. 關(guān)閉連接
      transport.close();
      
    }
  }

  /**
   * 創(chuàng)建一封只包含文本的簡單郵件
   *
   * @param session 和服務(wù)器交互的會話
   * @param sendMail 發(fā)件人郵箱
   * @param receiveMail 收件人郵箱
   * @return
   * @throws Exception
   */
  public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception {
    // 1. 創(chuàng)建一封郵件
    MimeMessage message = new MimeMessage(session);

    // 2. From: 發(fā)件人(昵稱有廣告嫌疑,避免被郵件服務(wù)器誤認(rèn)為是濫發(fā)廣告以至返回失敗,請修改昵稱)
    message.setFrom(new InternetAddress(sendMail, "you father", "UTF-8"));

    // 3. To: 收件人(可以增加多個收件人、抄送、密送)
    message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "XX用戶", "UTF-8"));

    // 4. Subject: 郵件主題(標(biāo)題有廣告嫌疑,避免被郵件服務(wù)器誤認(rèn)為是濫發(fā)廣告以至返回失敗,請修改標(biāo)題)
    message.setSubject("打折鉅惠", "UTF-8");

    // 5. Content: 郵件正文(可以使用html標(biāo)簽)(內(nèi)容有廣告嫌疑,避免被郵件服務(wù)器誤認(rèn)為是濫發(fā)廣告以至返回失敗,請修改發(fā)送內(nèi)容)
    message.setContent("新疆人用戶你好,快來買鞋,今天全場5折, 快來搶購, 錯過今天再等一年。。。emmm軟工實踐測試郵件", "text/html;charset=UTF-8");

    // 6. 設(shè)置發(fā)件時間
    message.setSentDate(new Date());

    // 7. 保存設(shè)置
    message.saveChanges();

    return message;
  }

如果本文大家還是有沒有理解,可以參考另外一篇相關(guān)文章:

Android通過startService播放背景音樂

相關(guān)文章

  • Android項目中g(shù)radle的執(zhí)行流程

    Android項目中g(shù)radle的執(zhí)行流程

    大家好,本篇文章主要講的是Android項目中g(shù)radle的執(zhí)行流程,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Android 彈出Dialog時隱藏狀態(tài)欄和底部導(dǎo)航欄的方法

    Android 彈出Dialog時隱藏狀態(tài)欄和底部導(dǎo)航欄的方法

    這篇文章主要介紹了Android 彈出Dialog時隱藏狀態(tài)欄和底部導(dǎo)航欄的實例代碼,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • flutter 輸入框組件TextField的實現(xiàn)代碼

    flutter 輸入框組件TextField的實現(xiàn)代碼

    這篇文章主要介紹了flutter 輸入框組件TextField的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Android Webview與ScrollView的滾動兼容及留白處理的方法

    Android Webview與ScrollView的滾動兼容及留白處理的方法

    本篇文章主要介紹了Android Webview與ScrollView的滾動兼容及留白處理的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android開發(fā)實現(xiàn)跟隨手指的小球效果示例

    Android開發(fā)實現(xiàn)跟隨手指的小球效果示例

    這篇文章主要介紹了Android開發(fā)實現(xiàn)跟隨手指的小球效果,涉及Android圖形繪制、事件響應(yīng)、界面布局等相關(guān)操作技巧,需要的朋友可以參考下
    2019-04-04
  • Android基于注解的6.0權(quán)限動態(tài)請求框架詳解

    Android基于注解的6.0權(quán)限動態(tài)請求框架詳解

    這篇文章主要介紹了Android基于注解的6.0權(quán)限動態(tài)請求框架詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Flutter彈性布局Flex水平排列Row垂直排列Column使用示例

    Flutter彈性布局Flex水平排列Row垂直排列Column使用示例

    這篇文章主要為大家介紹了Flutter彈性布局Flex水平排列Row垂直排列Column使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Android簡易電話撥號器實例詳解

    Android簡易電話撥號器實例詳解

    這篇文章主要為大家詳細(xì)介紹了Android簡易電話撥號器實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Flutter中數(shù)據(jù)庫的使用教程詳解

    Flutter中數(shù)據(jù)庫的使用教程詳解

    在Flutter開發(fā)過程中,有時需要對數(shù)據(jù)進(jìn)行本地的持久化存儲,使用sp文件形式雖然也能解決問題,但是有時數(shù)據(jù)量較大的時候,顯然我們文件形式就不太合適了,這時候我們就需要使用數(shù)據(jù)庫進(jìn)行存儲。本文將詳細(xì)講講Flutter中數(shù)據(jù)庫的使用,需要的可以參考一下
    2022-04-04
  • Android 畫一個太極圖實例代碼

    Android 畫一個太極圖實例代碼

    這篇文章主要介紹了Android 畫一個太極圖實例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-09-09

最新評論