基本上我只是试图从另一个Activity类中运行一个名为NotificationReceiverActivity的类,但是当我点击按钮运行活动类时屏幕上没有显示任何内容,我觉得我总是代码的xml部分的noob,这里是AndroidManifest.xml;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.notificationmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="de.vogella.android.notificationmanager.CreateNotificationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="de.vogella.android.notificationmanager.NotificationReceiverActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
CreateNotificationActivity类的主要活动,完美无缺地工作和开启。
主要活动的Java代码;
public class CreateNotificationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(0, noti);
}
}
NotificationReceiverActivity类;
public class NotificationReceiverActivity extends Activity implements OnClickListener {
private final String CLASSNAME = getClass().getSimpleName();
Camera cam = null;
ImageButton ib1;
Parameters para;
PowerManager pm;
WakeLock wl;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wl.acquire();
initialize();
ib1.setOnClickListener(this);
Log.i(CLASSNAME, "CREATING NOW"+cam);
}
private void initialize() {
// TODO Auto-generated method stub
ib1 = (ImageButton) findViewById(R.id.ib2);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (cam == null) {
cam = Camera.open();
para = cam.getParameters();
para.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(para);
Log.i(CLASSNAME, "AA"+cam);
} else {
para.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(para);
cam.release();
cam = null;
Log.i(CLASSNAME, "BB"+cam);
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release();
cam=cam;
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
cam=cam;
}
}
日志猫的前4行;
01-31 06:11:10.582: E/AndroidRuntime(29533): FATAL EXCEPTION: main
01-31 06:11:10.582: E/AndroidRuntime(29533): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.vogella.android.notificationmanager/de.vogella.android.notificationmanager.NotificationReceiverActivity}: java.lang.SecurityException: Neither user 10181 nor current process has android.permission.WAKE_LOCK.
01-31 06:11:10.582: E/AndroidRuntime(29533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
01-31 06:11:10.582: E/AndroidRuntime(29533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
答案 0 :(得分:2)
从NotificationReceiverActivity中删除intent过滤器。
使用按钮从您的CreateNotificationActivity启动NotificationReceiverActivity,您必须在活动中为该按钮指定clicklistener
这是
的一个例子public class CreateNotificationActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(CreateNotificationActivity.this,NotificationReceiverActivity.class);
startActivity(intent);
}
});
}
}