• Android学习笔记之Intent(2)


    打开网页

    package com.jiahemeikang.helloandroid;
    
    import java.io.File;
    
    import com.jiahemikang.service.EchoService;
    import com.jiahemikang.service.EchoService.EchoServiceBingder;
    import com.jikexuyua.broadcastreceiver.MyBC;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.provider.ContactsContract;
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.ServiceConnection;
    import android.database.Cursor;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements OnClickListener,ServiceConnection{
    
        private TextView tvResult ;
        private Intent serviceIntent;
        private Button btnStarAty1; 
        private Button btnStarService; 
        private Button btnStopService; 
        private Button btnAction;
        private Button btnBingService; 
        private Button btnUnBingService;
        private Button btnGetNum;
        private Button btnbroadcast;
        private Button regbroadcastButton;
        private Button btnOpenPic;
        private Button btnOpenUrl;
        private Button btnunregbroadcastButton;
        public EchoService echoService = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            serviceIntent = new Intent(this,EchoService.class);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tvResult = (TextView)findViewById(R.id.tvResult);
            
            btnStarAty1= (Button)findViewById(R.id.btnStartAty1);
            btnStarService= (Button)findViewById(R.id.btnStartService);
            btnStopService= (Button)findViewById(R.id.btnStopService);
            
            btnOpenPic= (Button)findViewById(R.id.btnOpenPic);
            btnOpenPic.setOnClickListener(this);
            btnOpenUrl= (Button)findViewById(R.id.btnOpenUrl);
            btnOpenUrl.setOnClickListener(this);
            
            btnStarService.setOnClickListener(this);
            btnStopService.setOnClickListener(this);
            
            
            btnBingService= (Button)findViewById(R.id.bingdingservice);
            btnUnBingService= (Button)findViewById(R.id.unbingdingservice);
            btnBingService.setOnClickListener(this);
            btnUnBingService.setOnClickListener(this);
            
            btnGetNum= (Button)findViewById(R.id.btnGetNum);
            btnGetNum.setOnClickListener(this);
            
            btnbroadcast= (Button)findViewById(R.id.btnbroadcast);
            btnbroadcast.setOnClickListener(this);
            
            btnAction= (Button)findViewById(R.id.btnAction);
            btnAction.setOnClickListener(this);
            
            regbroadcastButton= (Button)findViewById(R.id.btnregbroadcast);
            regbroadcastButton.setOnClickListener(this);
            btnunregbroadcastButton= (Button)findViewById(R.id.btnunregbroadcast);
            btnunregbroadcastButton.setOnClickListener(this);
            
            btnStarAty1.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    
                    Intent i = new Intent(MainActivity.this,Aty1.class);
                    i.putExtra("txt","Hello aty1");
                    startActivity(i);
                    startActivityForResult(i,0);
                }
            });
            Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            while (cursor.moveToNext()) {
                String _strName =cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                System.out.println(">>>>>>>>>>>>>>>"+_strName);
            }
        }
    
        @Override
        protected void onActivityResult(int a,int b ,Intent i){
            
            if (i!=null) {
                String result = i.getStringExtra("result");
                
                tvResult.setText(result);
            }
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        private  final MyBC myBC = new MyBC();
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.btnStartService:
                startService(serviceIntent);
                break;
            case R.id.btnStopService:
                stopService(serviceIntent);
                break;
            case R.id.bingdingservice:
                bindService(serviceIntent,this,Context.BIND_AUTO_CREATE);
                break;
            case R.id.unbingdingservice:
                unbindService(this);
                echoService =null;
                break;
            case R.id.btnGetNum:
                if (echoService!=null) {
                    System.out.print("当前服务的 数字为"+echoService.getCountNum());
                }
                break;
            case R.id.btnbroadcast:
                Intent i = new Intent(MyBC.ACTION);
                i.putExtra("txt", "消息");
                sendBroadcast(i);
                break;
            case R.id.btnregbroadcast:
                registerReceiver(myBC, new IntentFilter(MyBC.ACTION));
                break;
            case R.id.btnunregbroadcast:
                unregisterReceiver(myBC);
                break;
            case R.id.btnAction:
                Intent ii = new Intent("com.jikexueyuan.intent.action.Aty2");
                startActivity(ii);
                break;//btnOpenUrl
            case R.id.btnOpenPic:
                File file = new File("/mnt/sdcard/1.jpg");
                Intent intentfile = new Intent(Intent.ACTION_VIEW);
                intentfile.setDataAndType(Uri.fromFile(file), "image/*");
                startActivity(intentfile);
                break;
            case R.id.btnOpenUrl:
                System.out.print("打开网页.........");
                Intent intenturl = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.hao123.com/"));
                startActivity(intenturl);
                break;
            default:
                break;
            }
        }
    
        
        @Override
        public void onServiceConnected(ComponentName arg0, IBinder binder) {
            System.out.println("onServiceConnected");
            echoService = ((EchoService.EchoServiceBingder)binder).getService();
            
        }
    
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            // TODO Auto-generated method stub
            
        }
        
        
        
        //onStart
    
    }
  • 相关阅读:
    tomcat剖析(一)
    java内存区域
    经典排序算法-冒泡与选择
    使用docker安装mysql服务
    C语言博客作业--结构体
    C博客作业--指针
    C语言博客作业--字符数组
    C语言博客作业--数组
    C语言博客作业--数据类型
    C语言博客作业--函数
  • 原文地址:https://www.cnblogs.com/wuhailong/p/5248635.html
Copyright © 2020-2023  润新知