android開機啟動應用

來源:互聯網
上載者:User



 

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

下面這是主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");
        //這是網上有人用的方法,我用真機試過,用不了,可能My Phone被什麼阻止了,可能此方法還是可用,不過我覺得能用也覺得麻煩
//        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);
//----------------------------------------------------------------------
        //我試過的方法,我用My Phone也試不出,在模擬器上可以行得通
        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>

效果如下

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.