Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android開機啟動應用

android開機啟動應用

編輯:關於Android編程



 

今天在網上搜了下開機啟動應用的例子,有很多,發現有些真機還是不行,非要在模似器上才行。把一些我試過的代碼貼上來

下面這是主Activity,一打開讓它顯示系統的時間

package com.test;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class AtestActivity extends Activity {
    /** Called when the activity is first created. */
   
    private TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        tv = (TextView) findViewById(R.id.tv_test);
        Date date = new Date();
        SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String time = simp.format(date);
        tv.setText(time);
        tv.setTextSize(25);
        tv.setTextColor(Color.RED);
       
        KeyguardManager keyguardManager
        = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
        KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
         
        lock.disableKeyguard(); //禁用鍵盤鎖顯示。如果鍵盤鎖是目前顯示,隱藏它。
        
    }
}

下面這個是從BroadCastReceiver繼承的類

package com.test;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;

public class StartListenerActivity extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("receiver", "onReceiver begin");
        //這是網上有人用的方法,我用真機試過,用不了,可能我的手機被什麼阻止了,可能此方法還是可用,不過我覺得能用也覺得麻煩
//        PendingIntent pendingIntent = PendingIntent //不一定發生,不可預測的用此類
//                .getActivity(context, 0, new Intent(context,AtestActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//        
//        long time = SystemClock.elapsedRealtime();
//        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         //網上有人用此方法,只檢查一次
//        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pendingIntent);
        //5秒檢查一次,
//        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, 5 * 1000, pendingIntent);
//-----------------------------------------------------------------------------------
        //這是另一個方法,我在真機也用不了,模擬器上暫時沒試了
//        String category = "android.intent.category.LAUNCHER";
//        String action = "android.intent.action.MAIN";
//        Intent newintent = new Intent(context, AtestActivity.class);
//        newintent.setAction(action);
//        newintent.addCategory(category);
//        newintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//----------------------------------------------------------------------
        //我試過的方法,我用我的手機也試不出,在模擬器上可以行得通
        Intent newintent = new Intent(context, AtestActivity.class);
        newintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(newintent);
        Log.i("receiver","onReceiver over");
    }

}
配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
    <!-- 有人說要在這裡加下面一行,我沒加,也可以 -->
   
    <!--<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>  -->
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AtestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <receiver android:name=".StartListenerActivity">
           
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

效果如下

 

\
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved