当前位置: 编程技术>移动开发
本页文章导读:
▪障蔽HOME 屏蔽HOME
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
......
▪ 读写私有资料的函数 读写私有文件的函数
public String ReadSettings(Context context) { FileInputStream fIn = null; InputStreamReader isr = null; char[] inputBuffer = new char[255]; .........
▪ 运用BroadcastReceiver实现开机启动Service或Activity 使用BroadcastReceiver实现开机启动Service或Activity
使用BroadcastReceiver实现开机启动Service或Activity比起symbian和j2me,android还是很容易实现开机自动启动应用的: 首先必须有一个BroadcastReceiver以便.........
[1]障蔽HOME
来源: 互联网 发布时间: 2014-02-18
屏蔽HOME
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
[2] 读写私有资料的函数
来源: 互联网 发布时间: 2014-02-18
读写私有文件的函数
public String ReadSettings(Context context)
{
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[255];
String data = null;
try{
fIn = openFileInput("lee.txt");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
public void WriteSettings(String data)
{
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput("lee.txt",MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
//Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
//Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String ReadSettings(Context context)
{
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[255];
String data = null;
try{
fIn = openFileInput("lee.txt");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
public void WriteSettings(String data)
{
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput("lee.txt",MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
//Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
//Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[3] 运用BroadcastReceiver实现开机启动Service或Activity
来源: 互联网 发布时间: 2014-02-18
使用BroadcastReceiver实现开机启动Service或Activity
使用BroadcastReceiver实现开机启动Service或Activity
比起symbian和j2me,android还是很容易实现开机自动启动应用的:
首先必须有一个BroadcastReceiver以便监听手机 开机intent,
而该receiver又负责启动你的service或者activity.
public class yourReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(yourService.ACTION_START);
i.setClass(context, yourService.class);
context.startService(i);
}
}
其次,就需要在Manifest文件中声明一下intent-filter:
先加入使用权限声明:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
然后加入receiver 注册声明:
<receiver android:name=".yourReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
这样开机后yourReceiver 就能收到开机的intent,然后自动启动你的service 或activity.
还是Android好, 能够监听开机intent, j2me中一直无法实现开机自启动,通过Push Registry来实现自启动也好麻烦。 不知道Android中的这个user-permission是只需要这么声明就行了么。
使用BroadcastReceiver实现开机启动Service或Activity
比起symbian和j2me,android还是很容易实现开机自动启动应用的:
首先必须有一个BroadcastReceiver以便监听手机 开机intent,
而该receiver又负责启动你的service或者activity.
public class yourReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(yourService.ACTION_START);
i.setClass(context, yourService.class);
context.startService(i);
}
}
其次,就需要在Manifest文件中声明一下intent-filter:
先加入使用权限声明:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
然后加入receiver 注册声明:
<receiver android:name=".yourReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
这样开机后yourReceiver 就能收到开机的intent,然后自动启动你的service 或activity.
还是Android好, 能够监听开机intent, j2me中一直无法实现开机自启动,通过Push Registry来实现自启动也好麻烦。 不知道Android中的这个user-permission是只需要这么声明就行了么。
最新技术文章: