Android 利用Service BroadcastReceiver實現小例子

來源:互聯網
上載者:User

標籤:

Activity:

 1 package com.example.test; 2  3 import android.app.Activity; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.IntentFilter; 7 import android.os.Bundle; 8 import android.view.Menu; 9 import android.view.MenuItem;10 import android.widget.TextView;11 /**12  * 實現當插接電源時,手機震動並彈出訊息。13  * @author shaobn14  * @date 2015-9-1915  * @packege com.example.testTest16  */17 public class MainActivity extends Activity {18     private TextView text;19     @Override20     protected void onCreate(Bundle savedInstanceState) {21         super.onCreate(savedInstanceState);22         setContentView(R.layout.activity_main);23         text = (TextView) this.findViewById(R.id.textView1);24         IntentFilter intentFilter = new IntentFilter();25         intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);26         MyReceiver myReceiver = new MyReceiver();27         registerReceiver(myReceiver,intentFilter);28     }29 }

BroacastReceiver:

 1 package com.example.test; 2  3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 /** 7  *  8  * @author shaobn 9  * @date 2015-9-1910  * @packege com.example.testTest11  */12 public class MyReceiver extends BroadcastReceiver {13 14     @Override15     public void onReceive(Context context, Intent arg1) {16         // TODO Auto-generated method stub17         Intent intent = new Intent();18         intent.putExtra("msg","helloworld");19         intent.setClass(context,MyService.class);20         context.startService(intent);21     }22 }

Service:

 1 package com.example.test; 2  3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.IBinder; 6 import android.os.Vibrator; 7 import android.widget.Toast; 8 /** 9  * 10  * @author shaobn11  * @date 2015-9-1912  * @packege com.example.testTest13  */14 public class MyService extends Service {15 16     @Override17     public IBinder onBind(Intent arg0) {18         // TODO Auto-generated method stub19         return null;20     }21     @Override22     public void onCreate() {23         // TODO Auto-generated method stub24         super.onCreate();25     }26     @Override27     public int onStartCommand(Intent intent, int flags, int startId) {28         // TODO Auto-generated method stub29         String message = intent.getStringExtra("msg");30         Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);31         vibrator.vibrate(2000);32         Toast.makeText(getApplicationContext(),message,1).show();33         return super.onStartCommand(intent, flags, startId);34     }35 }

自己隨手寫的,程式有點簡單,就不寫注釋了。AndroidManifest.xml中注意配置Service 以及vibrator許可權。

Android 利用Service BroadcastReceiver實現小例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.