Thao tác chuyển hướng với Activity trong Android

Thao tác chuyển hướng với Activity trong Android

Mục tiêu:

Tạo một màn hình chào sau khi người dùng chạy ứng dụng. Sau 5 giây tự động chuyển đến màn hình chính

Bước 1: Tạo Activity mới

Click phải chuột thư mục src, chon New->Other->Android->AndroidActivity.
Đặt tên cho Activity mới là manhinhchaoActivity. Bấm Finish

Bước 2: Cấu hình cho activity màn hình chào hiện lên trước nhất khi chạy ứng dụng

Vào file AndroidManifest.xml, chỉnh code như sau:

       <activity
            android:name="com.example.taomanhinhchao.manhinhchaoActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>        
        <activity
            android:name="com.example.taomanhinhchao.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.taomanhinhchao.MAINACTIVITY" />                 <category android:name=”android.intent.category.DEFAULT” />
            </intent-filter>
        </activity>  

Bước 3: Thiết lập sao cho sau 5 giây màn hình chào chuyển đến màn hình chính
Dùng Thread để cài thời gian, dùng hàm sleep đặt khoảng thời gian ngủ là 5 giây, sau khi kết thúc hẹn giờ finally gọi Activity chính ra.

Mở file manhinhchaoActivity.java, ta viết thêm mã lệnh trong hàm onCreate như sau:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manhinhchao);
    //Dùng cài đặt sau 5 giây màn hình tự chuyển
        Thread bamgio=new Thread(){
            public void run()
            {
                try {
                    sleep(5000);
                } catch (Exception e) {
                    
                }
                finally
                {
                    Intent activitymoi=new Intent("com.example.taomanhinhchao.MAINACTIVITY");
                    startActivity(activitymoi);
                }
            }
        };
        bamgio.start();        
    }
    //sau khi chuyển sang màn hình chính, kết thúc màn hình chào
    protected void onPause(){
        super.onPause();
        finish();
    }
}  

Chạy thử.

Bạn thấy bài viết này như thế nào?: 
Average: 8.5 (2 votes)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Giới thiệu Drupal Views Templates và Theming

Giới thiệu sự kết hợp giữa Drupal Views Templates và Theming

One of our students is learning Drupal and trying to master Views. They wanted to know how to style different areas of each Views. We wrote this tutorial as an introduction for them to templates and theming for Views.

5 Phần mềm chỉnh sửa âm thanh miễn phí tốt nhất

5 Phần mềm chỉnh sửa âm thanh miễn phí tốt nhất

Không cần phải bỏ tiền túi để mua phần mềm chỉnh sửa ngon lành nhưng bạn vẫn có thể cho ra những sản phẩm ấn tượng với 5 phần mềm chỉnh sửa âm thanh miễn phí tốt nhất dưới đây.

Giới thiệu 4 Social Sharing Modules cho Drupal

Giới thiệu 4 Social Sharing Modules cho Drupal

If you don't give your site visitors an opportunity to share your webpages, you're missing out on an opportunity for others to hear about your great content.

Công ty diệt chuột T&C

 

Diet con trung