活动开始在手机中的Android启动 [英] start Activity at startup of phone in android
问题描述
我试图启动活动在启动手机,但整个程序是不是运行没有在程序中的任何错误,请参阅我的编码波纹管(或这里的 http://pastebin.com/BKaE4AaU ):
I am trying to start activity at start up of phone but whole program is not running there is a no error in program , see my coding bellow(or here http://pastebin.com/BKaE4AaU):
autostart.java 的
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
public class autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Intent intent = new Intent(arg0,service.class);
arg0.startService(intent);
Log.i("Autostart", "started");
}
}
service.java 的
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class service extends Service
{
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(),hello.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
hello.java 的
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class hello extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
}
}
manifest.java是如下的
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".autostart"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
推荐答案
您的XML应该存储在一个名为AndroidManifest.xml中,文件中没有manifest.java。
Your XML should be stored in a file named AndroidManifest.xml, not manifest.java.
另外一个原因,你的code没有运行,可能是您的应用程序安装在外部存储(SD卡)。 BOOT_COMPLETE发送到应用程序的外部存储设备安装前。因此,如果应用程序被安装到外部存储它不会收到BOOT_COMPLETE广播消息。
Another reason your code is not being run, might be that your App is installed on external storage (sdcard). BOOT_COMPLETE is sent to applications before external storage is mounted. So if application is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
如果这不是问题,已经有一个的如何获得启动完成接收工作在Android 很好的说明。
If that isn't the problem, there is already a very good description of how to get boot completed receivers working on Android.
这篇关于活动开始在手机中的Android启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!